#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node* next;
};
struct node*createnode(int data){
    struct node* newnode=(struct node*)malloc(sizeof(struct node));
    newnode->data=data;
    newnode->next=NULL;
    return newnode;
}
void push(struct node**top,int data){
    struct node*newnode=createnode(data);
    newnode->next=*top;
    *top=newnode;
}
void findminmax(struct node*top,int*min,int*max){
    *min=*max=top->data;
    struct node*temp=top;
    while(temp!=NULL){
        if(temp->data<*min)*min=temp->data;
        if(temp->data>*max)*max=temp->data;
        temp=temp->next;
    }
}
int main(){
    int k;
    if(scanf("%d",&k)!=1 || k < 4 || k > 12)
    printf("Invalid input\n");
    return 0;
}
int pulses[k];
for(int i=0;i<k;i++){
    if(scanf("%d",&pulses[i])!=1 || pulses[i] < 1 || pulses[i] > 5000){
        printf("Invalid input\n");
        return 0;
    }
}
struct node*stack=NULL;
int sum=0;
for(int i=0;i<k;i++){
    if(pulses[i] > sum){
        push(&stack,pulses[i]);
        sum+=pulses[i];
    }
}
if(stack==NULL || stack->next==NULL){
    printf("0\n");
    return 0;
 }
 int min,max;
 findminmax(stack,&min,&max);
 printf("%d\n", max - min);
 return 0;
}