#include <bits/stdc++.h>
using namespace std;

long long n, a[1000003], ans=-1000000000;

int main () {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);

cin >> n;

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

for (long long i=1; i < n; i++) {
    long long max1=a[i], max2=-1000000000;
    for (long long j=i-1; j >= 0; j--) {
        if (a[j] >= max1) {
            max2 = max1;
            max1 = a[j];
        }
        else if (a[j] < max1 and a[j] >= max2) {
            max2 = a[j];
        }
        ans = max (ans, max2 - (i-j+1));
        if (a[i] < max2) {break;}
    }
}

cout << ans << endl;

return 0;
}
