// zad1
#include<bits/stdc++.h>
using namespace std;

struct Cell{
    int nach, sum;
    int pnext, pprev;
    bool valid;
};


const int maxn = 1000000 + 4;
int n;
int a[maxn];
Cell b[maxn];



int main() {
    //cout << "--- zad1 ---" << endl;
    cin.tie(0);
    cout.tie(0);

    cin >> n;
    for(int i=1;i<=n;i++){
        cin >> a[i];
    }


    int maxnadeasno = -1;
    { // nadeasno
        for(int i=1;i<=n;i++){
            b[i].nach = a[i];
            b[i].sum = b[i].nach;
            b[i].pnext = i;
            b[i].pprev = i - 1;
            b[i].valid = true;
        }

        b[0].pnext = 1;
        b[0].nach = 999999999999;

        for(int i=1;i<=n;i++){
            b[i].sum -= 1;
            int k = b[0].pnext;
            while(true){
                if(k == i) break;
                if(b[k].valid) {
                    if(b[k].nach <= b[i].nach){
                        b[k].valid = false;
                        b[b[k].pprev].pnext = b[i].pnext;
                        b[b[i].pnext].pprev = b[i].pprev;
                    }else{
                        b[k].sum -= 1;
                    }
                }
                k = b[k].pnext;
            }
            b[i].pnext = i + 1;
        }

        for(int i=1;i<=n;i++){
            if(b[i].sum > maxnadeasno && b[i].valid == false){
                maxnadeasno = b[i].sum;
            }
        }
    }



    int maxnaleavo = -1;
    { // naleavo
        for(int i=1;i<=n;i++){
            b[i].nach = a[n - i + 1];
            b[i].sum = b[i].nach;
            b[i].pnext = i;
            b[i].pprev = i - 1;
            b[i].valid = true;
        }

        b[0].pnext = 1;
        b[0].nach = 999999999999;

        for(int i=1;i<=n;i++){
            b[i].sum -= 1;
            int k = b[0].pnext;
            while(true){
                if(k == i) break;
                if(b[k].valid) {
                    if(b[k].nach <= b[i].nach){
                        b[k].valid = false;
                        b[b[k].pprev].pnext = b[i].pnext;
                        b[b[i].pnext].pprev = b[i].pprev;
                    }else{
                        b[k].sum -= 1;
                    }
                }
                k = b[k].pnext;
            }
            b[i].pnext = i + 1;
        }

        for(int i=1;i<=n;i++){
            if(b[i].sum > maxnaleavo && b[i].valid == false){
                maxnaleavo = b[i].sum;
            }
        }
    }

    //cout << maxnadeasno - 1 << " " << maxnaleavo - 1 << endl;
    cout << max(maxnadeasno, maxnaleavo) - 1;

    return 0;
}
