#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const double EPS = 1e-10;
const int N = 20;
int n;
int a[1 << N];
int maxTree;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  maxTree = (1 << (n + 1)) - 1;
  for (int i = 2; i <= maxTree; ++i) {
    cin >> a[i];
  }
  long long res = 0;
  for (int i = (1 << n) - 1; i > 0; --i) {
    int l = a[i * 2];
    int r = a[i * 2 + 1];
    a[i] += max(l, r);
    res += abs(l - r);
  }
  cout << res;
}
