#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 = *top;
    *top = newNode;
}
void findMixMax(struct Node* top, int* min, int* max) {
    *min = *max = top->data;
    struct node* temp = top;
    while (term != 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;
    forn(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;
}