#include <bits/stdc++.h>

using namespace std;

typedef int ll;

const ll Size=1e6+1;
ll a[Size];
ll dp[Size],l[Size],r[Size];
ll n;

void solve(){
    cin>>n;
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    stack<pair<ll,ll>> largest;
    for(ll i=0;i<n;i++){
        while(!largest.empty() and largest.top().first<a[i]){
            largest.pop();
        }
        if(largest.empty()){
            l[i]=-1;
        }else{
            l[i]=largest.top().second;
        }
        largest.push({a[i],i});
    }
    while(!largest.empty()){
        largest.pop();
    }
    for(ll i=n-1;i>=0;i--){
        while(!largest.empty() and largest.top().first<a[i]){
            largest.pop();
        }
        if(largest.empty()){
            r[i]=n;
        }else{
            r[i]=largest.top().second;
        }
        largest.push({a[i],i});
    }
    ll ans=-1e18;
    for(ll i=0;i<n;i++){
        if(l[i]!=-1){
            ans=max(ans,a[i]-i+l[i]-1);
        }
        if(r[i]!=n){
            ans=max(ans,a[i]-r[i]+i-1);
        }
    }
    cout<<ans<<endl;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t=1;
    while(t--){
        solve();
    }
    return 0;
}
