#include using namespace std; #define ll long long #define pb push_back #define mp make_pair #define pii pair #define pll pair #define all(x) (x).begin(), (x).end() #define fi first #define se second const int nmax = 100005; int n, a[nmax], logg[nmax]; ll sol = -(1LL << 40); deque q; int main() { cin.sync_with_stdio(false); // freopen("a.in", "r", stdin); // freopen("a.out", "w", stdout); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 2; i <= n; i++) logg[i] = logg[i / 2] + 1; for (int lg = 0; (1 << lg) <= n; lg++) { int i = 1; q.clear(); q.pb(1); for (int j = 2; j <= n; j++) { while (logg[j - i] > lg) { if (!q.empty() && q.front() == i) q.pop_front(); i++; } ll mlc = a[j]; mlc += a[q.front()]; mlc -= lg; sol = max(sol, mlc); while (!q.empty() && a[q.back()] <= a[j]) q.pop_back(); q.pb(j); } } cout << sol; return 0; }