#include <bits/stdc++.h>   using namespace std;   const int N = 2300;   int dp[N], a[N], res = 0, n;   void dfs(int u, int d) {     if (d == n) {       dp[u] = 0;       return;     }     int l = u * 2, r = l + 1;     dfs(l, d + 1);     dfs(r, d + 1);     int x = dp[l] + a[l], y = dp[r] + a[r];     if (x < y) swap(x, y);     dp[u] = x;     res += x - y;   }   int main() {     cin >> n;     for (int i = 2; i < (1 << n + 1); i++) {       scanf( %d , &a[i]);     }     dfs(1, 0);     cout << res << endl;   }   