#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000;
int L[maxn];
int M[maxn];
int cnt;
int n;
int fun(int x) {
  if (x >= (1 << n)) return 0;
  int l = fun(x * 2) + L[x * 2];
  int r = fun(x * 2 + 1) + L[x * 2 + 1];
  if (l > r) {
    cnt += l - r;
    return l;
  } else {
    cnt += r - l;
    return r;
  }
}
int main() {
  cnt = 0;
  cin >> n;
  int p = (1 << (n + 1)) - 1;
  int maxx = 0;
  for (int i = 2; i <= p; i++) {
    cin >> L[i];
  }
  fun(1);
  cout << cnt << endl;
}
