#include <bits/stdc++.h>
using namespace std;
const long long infi = 1000000000000000009;
int ar[5009], n, tot, ans;
int doit(int curr) {
  if (curr > tot) return ar[curr];
  int done1 = doit(2 * curr);
  int done2 = doit(2 * curr + 1);
  ans = ans + max(done2, done1) - min(done1, done2);
  int ret = ar[curr] + max(done2, done1);
  return ret;
}
int main() {
  int i, j;
  scanf("%d", &n);
  tot = pow(2, n + 1) - 1;
  for (i = 2; i <= tot; i++) scanf("%d", &ar[i]);
  tot = pow(2, n) - 1;
  doit(1);
  printf("%d\n", ans);
  return 0;
}
