#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 (int i=0; i < n; i++) {
    multiset <int> s;
    s.insert(a[i]);
    for (int j=i+1; j < n; j++) {
        s.insert(a[j]);
        auto it = s.end();
        it--;
        it--;
        //cout << i << " " << j << " " << *it << " " << j-i+1 << endl;
        ans = max (ans, (long long) (*it) - (j-i+1));
    }
}

cout << ans << endl;

return 0;
}
