#include //#define fin cin //#define fout cout #define F first #define S second using namespace std; typedef long long ll; const int Nmax = 1e5 + 10; const int inf = 0x3f3f3f3f; long long ans; int n , i , L , R , crt , k , p; int a[Nmax]; int d[18][Nmax]; int Log[Nmax]; void makeRmq() { int i , j; for (i = 2; i <= n; ++i) Log[i] = Log[i>>1] + 1; for (i = 1; i <= n; ++i) d[0][i] = a[i]; for (i = 1; i <= Log[n]; ++i) for (j = 1; j <= n - (1 << i) + 1; ++j) d[i][j] = max(d[i-1][j] , d[i-1][j+(1<<(i-1))]); } int main() { #ifndef ONLINE_JUDGE freopen("input.in","r",stdin); freopen("output.out","w",stdout); #endif // ONLINE_JUDGE /* #ifndef ONLINE_JUDGE ifstream fin("input.in"); ofstream fout("output.out"); #endif // ONLINE_JUDGE */ scanf("%d", &n); for (i = 1; i <= n; ++i) scanf("%d", a + i); makeRmq(); ans = LLONG_MIN; for (i = 1;i <= n; ++i) { L = i; crt = INT_MIN; bool ok = 1; for (p = 0; ok; ++p) { L = i - (1 << (p + 1)) + 1; R = i - (1 << p); if (L <= 0) ok = 0, L = 1; if (R <= 0) R = 1; k = Log[R-L+1]; crt = max(crt , max(d[k][L] , d[k][R-(1< 1) ans = max(ans , 1LL * crt + 1LL * a[i]); } cout << ans << '\n'; return 0; }