#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=-1,i,j,z;

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++){
        for(j=i+1;j<n;j++){
            max1=0;
            max2=0;
            for(z=i;z<=j;z++){
                if(a[z]>max1){
                    max2=max1;
                    max1=a[z];
                }else if(a[z]>max2){
                    max2=a[z];
                }
            }
            int len=j-i+1;
            if(max2-len>ans){
                ans=max2-len;
            }
        }
    }
    cout << ans << endl;
}
/*
5
3 1 4 1 59
*/
