#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1 << 13;
int n;
int cnt[MAXN];
int fr[MAXN];
int sum_from_root[MAXN];
int mx_from_bot[MAXN];
int added[MAXN];
int main() {
  cin >> n;
  for (int i = 2; i <= (1 << (n + 1)) - 1; i++) {
    cin >> cnt[i];
  }
  int max_cnt = 0;
  for (int i = (1 << n); i <= (1 << (n + 1)) - 1; i++) {
    int v = i;
    while (v > 1) {
      fr[i] += cnt[v];
      v >>= 1;
    }
    max_cnt = max(fr[i], max_cnt);
  }
  int total = 0;
  for (int i = 2; i <= (1 << (n + 1)) - 1; i++)
    sum_from_root[i] = sum_from_root[i >> 1] + cnt[i];
  for (int i = (1 << (n + 1)) - 1; i >= 1; i--)
    mx_from_bot[i] = max(mx_from_bot[i << 1] + cnt[i << 1],
                         mx_from_bot[i << 1 | 1] + cnt[i << 1 | 1]);
  for (int i = 2, delta; i <= (1 << (n + 1)) - 1; i++) {
    delta = max_cnt - mx_from_bot[i] - sum_from_root[i] - added[i];
    if (delta >= 0) {
      total += delta;
      added[i << 1] = delta + added[i];
      added[i << 1 | 1] = delta + added[i];
    }
  }
  printf("%d\n", total);
  return 0;
}
