#include <iostream>
#include <stack>
#define endl '\n'
using namespace std;

const int MAXN = 1000000;

int a[MAXN + 5];
int le[MAXN + 5], ri[MAXN + 5];

int main()
{
    ios :: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int n;
    cin >> n;

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

    stack<int> st;
    st.push(0);
    for (int i = 1; i <= n; ++ i)
    {
        while (a[st.top()] < a[i]) st.pop();
        le[i] = st.top();
        st.push(i);
    }

    while (!st.empty()) st.pop();
    st.push(n + 1);
    for (int i = n; i >= 1; -- i)
    {
        while (a[st.top()] < a[i]) st.pop();
        ri[i] = st.top();
        st.push(i);
    }

    int ans = -1e7;
    for (int i = 1; i <= n; ++ i)
    {
        if (le[i] != 0) ans = max(ans, a[i] - (i - le[i] + 1));
        if (ri[i] != n + 1) ans = max(ans, a[i] - (ri[i] - i + 1));
    }

    cout << ans << endl;

    return 0;
}
