#include <bits/stdc++.h>   using namespace std;   int ans = 0;   int n;   int tree[10001];   int dp[10001] = {0};   int main() {     cin >> n;     int a[100001];     int tot = pow(2, n + 1) - 2;     for (int i = 2; i <= tot + 1; i++) {       cin >> a[i];     }     tree[0] = 0;     tree[1] = 0;     int ma = 0;     for (int i = 2; i <= pow(2, n + 1) - 1; i++) {       tree[i] = tree[i / 2] + a[i];       ma = max(tree[i], ma);     }     for (int i = n - 1; i >= 1; i--) {       for (int j = pow(2, n); j < pow(2, n + 1); j += pow(2, i)) {         int mi = ma - tree[j];         for (int k = 0; k < pow(2, i); k++) {           mi = min(mi, ma - tree[j + k]);         }         ans += mi;         for (int k = 0; k < pow(2, i); k++) {           tree[j + k] += mi;         }       }     }     for (int i = pow(2, n); i < pow(2, n + 1); i++) {       ans += (ma - tree[i]);     }     cout << ans << endl;     return 0;   }   