#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000 + 10;   const int INF = 1000000000;   const int MOD = (int)(1e9) + 7;   int main() {     int n;     cin >> n;     int m = (1 << (n + 1)) - 1;     vector<int> a(m + 10, 0);     for (int i = 2; i <= m; i++) cin >> a[i];     vector<int> s(m + 10, 0), c(m + 10, 0), add(m + 10, 0);     int l = 0;     for (int i = 2; i <= m; i++) {       s[i] = s[i / 2] + a[i];       l = max(l, s[i]);     }     for (int i = m; i >= 1; i--) {       if (i + i + 1 > m)         c[i] = s[i];       else         c[i] = max(c[i + i], c[i + i + 1]);     }     int res = 0;     for (int i = 2; i <= m; i++) {       int cur = c[i] + add[i / 2];       int inc = l - cur;       add[i] = add[i / 2] + inc;       res += inc;     }     cout << res << endl;   }   