#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;

const ll MAX_N = 1e6 + 10;
ll arr[MAX_N], n;

ll find_ans() {
    stack<ll> st;
    ll ans = 0;
    for(ll i = 0; i < n; i ++) {
        while(!st.empty() && arr[st.top()] < arr[i]) {
            st.pop();
        }

        if(!st.empty()) {
            ans = max(ans, arr[i] - (i - st.top() + 1));
        }
        st.push(i);
    }
    return ans;
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    cin >> n;
    assert(n != 1);
    for(ll i = 0; i < n; i ++) {
        cin >> arr[i];
    }

    ll ans = find_ans();
    reverse(arr, arr + n);
    ans = max(ans, find_ans());

    cout << ans << endl;

    return 0;
}
