#include<iostream>
#include<stack>
using namespace  std;

long long broiChisla;

long long chisla[5010];

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>broiChisla;

    for(long long i = 0; i < broiChisla; i++){
        cin>>chisla[i];
    }

    stack<int> s;

    int naiDobroto = -1000000010;

    for(int i = 0; i < broiChisla; i++){
        while(s.empty() == false){
            if(chisla[s.top()] < chisla[i]){
                int tuka = chisla[s.top()] - (i - s.top() + 1);
                naiDobroto = max(naiDobroto, tuka);
                s.pop();
            }else{
                break;
            }
        }
        s.push(i);
    }

    for(int i = broiChisla - 1; i >= 0; i--){
        while(s.empty() == false){
            if(chisla[s.top()] < chisla[i]){
                int tuka = chisla[s.top()] - (s.top() - i + 1);
                naiDobroto = max(naiDobroto, tuka);
                s.pop();
            }else{
                break;
            }
        }
        s.push(i);
    }

    cout<<naiDobroto<<endl;

    return 0;
}
