#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair P; int n; ll a[(1<<18)+1]; const int d=18; ll dp[37][100010]; ll solve(int s, int x){ if(x>n) return 0; if(dp[s+d][x]>=0) return dp[s+d][x]; ll s1=solve(s+1, 2*x)+a[2*x]*(s+1), s2=solve(s-1, 2*x+1)+a[2*x+1]*(s-1); ll s3=solve(s-1, 2*x)+a[2*x]*(s-1), s4=solve(s+1, 2*x+1)+a[2*x+1]*(s+1); ll ans=max({0ll, s1, s1+s2, s1, s3+s4, s4}); return dp[s+d][x]=ans; } int main() { cin>>n; for(int i=1; i<=n; i++) cin>>a[i]; for(int i=1; i<=n; i++) for(int j=0; j<=2*d; j++) dp[j][i]=-1; cout<