#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

int main() {
    int k;
    if (scanf("%d", &k) != 1) {
        printf("Invalid input");
        return 0;
    }

    if (k < 4 || k > 12) {
        printf("Invalid input");
        return 0;
    }

    struct Node* head = NULL;
    struct Node* tail = NULL;
    int value;

    for (int i = 0; i < k; i++) {
        if (scanf("%d", &value) != 1 || value < 1 || value > 5000) {
            printf("Invalid input");
            return 0;
        }
        struct Node* newNode = createNode(value);
        if (head == NULL) {
            head = tail = newNode;
        } else {
            tail->next = newNode;
            tail = newNode;
        }
    }

    // Now process the sequence
    struct Node* curr = head;
    struct Node* selected = NULL;
    struct Node* selTail = NULL;

    int sum = 0;
    while (curr != NULL) {
        if (curr->data > sum) {
            // Select this pulse
            struct Node* newNode = createNode(curr->data);
            if (selected == NULL) {
                selected = selTail = newNode;
            } else {
                selTail->next = newNode;
                selTail = newNode;
            }
            sum += curr->data;
        }
        curr = curr->next;
    }

    if (selected == NULL) {
        printf("Invalid input");
        return 0;
    }

    // Find min and max of selected
    int min = selected->data, max = selected->data;
    curr = selected->next;
    while (curr != NULL) {
        if (curr->data < min) min = curr->data;
        if (curr->data > max) max = curr->data;
        curr = curr->next;
    }

    printf("%d", max - min);

    return 0;
}

==============================================================================================================================================================================================