#include <bits/stdc++.h>

using namespace std;

int rmq[18][100010];
int v[100010];
int lg[100010];

int getMax(int xx, int yy) {
    int foo = yy - xx + 1;
    foo = lg[foo];
    return max(rmq[foo][xx], rmq[foo][yy - (1 << foo) + 1]);
}

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

    for (int i = 2; i <= n; ++i)
        lg[i] = lg[i / 2] + 1;
    for (int i = 1; i <= n; ++i)
        rmq[0][i] = v[i];

    int pw;
    for (pw = 1; (1 << pw) <= n; ++pw)
        for (int i = 1; i <= n; ++i) {
            rmq[pw][i] = rmq[pw - 1][i];
            if (i + (1 << (pw - 1)) <= n)
                rmq[pw][i] = max(rmq[pw - 1][i], rmq[pw - 1][i + (1 << (pw - 1))]);
        }
    long long res = -1LL << 50;

    for (pw = 0; (1 << pw) <= n; ++pw) {
        for (int i = 1; i <= n; ++i) {
            int lft = i + (1 << pw);
            if (lft <= n) {
                int rgt = i + (1 << (pw + 1)) - 1;
                if (rgt > n)
                    rgt = n;
                long long foo = (long long) v[i] + getMax(lft, rgt) - pw;
                if (foo > res)
                    res = foo;
            }
        }
    }
    cout << res;
    return 0;
}