#include <iostream>
#include <algorithm>

using namespace std;


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

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 sorted_row[N];
            copy_row(sorted_row, row+l, len);
            sort(sorted_row, sorted_row + len);
            if (max_minus_len == 0) {
                max_minus_len = sorted_row[len-2] - len;

            } else {
                if (sorted_row[len-2] - len > max_minus_len) {
                    max_minus_len = sorted_row[len-2] - 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;
}
