#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int MAX = 1e5 + 55;   const int INF = 1e9 + 77;   const long long INFINF = 1e18 + 1e17;   const double PI = acos(-1.0);   long long power(long long x, long long y) {     if (y == 0) return 1;     long long s = power(x, y / 2);     s *= s;     if (y & 1) s *= x;     return s;   }   int n;   long long e[4000][4000];   long long dis[4000];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     int node = 1;     for (int p = 1; p <= ((1 << n + 1) - 1) / 2; p++) {       int dis1;       int dis2;       cin >> dis1;       cin >> dis2;       e[node * 2][node] = dis1;       e[node * 2 + 1][node] = dis2;       node++;     }     long long ans = 0;     node = ((1 << n + 1) - 1);     while (node != 1) {       int node1 = node;       int node2 = node - 1;       int par = node / 2;       int max1 = dis[node1] + e[node1][par];       int max2 = dis[node2] + e[node2][par];       dis[par] = max(max1, max2);       ans += (dis[par] - min(max1, max2));       node -= 2;     }     cout << ans;     return 0;   }   