#include using namespace std; int main() { //ifstream cin("testD.in"); int n; cin >> n; //ios_base :: sync_with_stdio(false); vector v(n, 0); for(int i = 0; i < n; ++i) cin >> v[i]; long long ans = -1e10; for(int log = 0; (1 << log) <= n; ++log) { int length = (1 << log); deque D; int accept = 2 * length - 1; for(int i = 0; i < n; ++i) { while(!D.empty() && i - D.front() > accept) D.pop_front(); if(!D.empty()) ans = max(ans, v[i] + v[D.front()] - log + 0LL); while(!D.empty() && v[i] > v[D.back()]) { D.pop_back(); } D.push_back(i); } } cout << ans << "\n"; }