#include <iostream>

using namespace std;


void print_row(int *row, int N) {
    for (int i = 0; i < N; i++) {
        cout << row[i] << " ";
    }
    //cout << "\n";
}

void copy_row(int *dest, int *src, int N) {
    for (int i = 0; i < N; i++) {
        dest[i] = src[i];
    }
}

int max2(int *row, int N) {
    int last_max = row[0];
    int before_last = row[1];
    for (int i = 0; i < N; i++) {
        if (row[i] > last_max) {
            before_last = last_max;
            last_max = row[i];
        }
    }
    return before_last;
}

int subrow(int *row, int N) {
    int max_minus_len;
    for (int len = 2; len <= N; len++) {
        for (int i = 0; i <= N-len; i++) {
            int l = i;
            int r = i+len-1;
            int subrow[len];
            copy_row(subrow, row+l, len);

            //int sorted_row[N];
            //copy_row(sorted_row, subrow, len);
            //sort(sorted_row, sorted_row + len);
            //print_row(subrow, len);
            //cout <<  "max2: " << max2(subrow, len) << "\n";
            if (max_minus_len == 0) {
                max_minus_len = max2(subrow, len) - len;

            } else {
                if (max2(subrow, len) - len > max_minus_len) {
                    max_minus_len = max2(subrow, len) - len;
                }
            }
        }
    }

    return max_minus_len;
}

int main() {

    int N;
    cin >> N;
    int a[N];
    for (int i = 0; i < N; i++) {
        cin >> a[i];
    }

    cout << subrow(a, N) << "\n";

    return 0;
}
