#include <iostream>
#define endl "\n"
#define ll long long
using namespace std;

const ll maxn=1e6+1;
ll n,a[maxn],max1=0,max2=0,ans=-maxn,i,j,z;
ll v[2*maxn];

int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
    cin >> n;
    for(i=0;i<n;i++){
        cin >> a[i];
    }
    for(i=0;i<n;i++){
        max1=a[i];
        max2=-maxn;
        for(j=i+1;j<n;j++){
            if(a[j]>max1){
                max2=max1;
                max1=a[j];
            }else if(a[j]>max2){
                max2=a[j];
            }
            int len=j-i+1;
            //cout << max1 << " " << max2 << " " << i << " " << j << endl;
            if(max2-len>ans){
                ans=max2-len;
            }
        }
    }
    cout << ans << endl;
}

/*#include <iostream>
#include <vector>
#define endl "\n"
#define ll long long
using namespace std;

const ll maxn=1e6+1;
ll n,a[maxn],max1=0,max2=0,ans=-1,i,j,z;
vector<ll> loc;

int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
    cin >> n;
    for(i=0;i<n;i++){
        cin >> a[i];
    }
    for(i=0;i<n;i++){
        if(i==0 and a[i]>a[i+1]){
            loc.push_back(i);
            continue;
        }
        if(i==n-1 and a[i]>a[i-1]){
            loc.push_back(i);
            continue;
        }
        if(a[i]>a[i+1] and a[i]>a[i-1]){
            loc.push_back(i);
        }
    }
    for(i=0;i<loc.size();i++){
        for(j=i+1;j<loc.size();j++){
            max1=0;
            max2=0;
            for(z=loc[i];z<=loc[j];z++){
                if(a[z]>max1){
                    max2=max1;
                    max1=a[z];
                }else if(a[z]>max2){
                    max2=a[z];
                }
            }
            int len=loc[j]-loc[i]+1;
            if(max2-len>ans){
                ans=max2-len;
            }
        }
    }
    cout << ans << endl;
}
/*
5
3 1 4 1 59
*/

