#include <bits/stdc++.h>
using namespace std;
const int maxn = 1500;
int ai[maxn];
int n;
int sum;
int ans;
int DFS(int now) {
  if (now > sum) {
    return 0;
  }
  int l = DFS(now * 2);
  int r = DFS(now * 2 + 1);
  ans += abs(l - r);
  return ai[now] + max(l, r);
}
void init() {
  scanf("%d", &n);
  sum = (1 << (n + 1)) - 1;
  for (int i = 2; i <= sum; i++) {
    scanf("%d", &ai[i]);
  }
}
void play() {
  ans = 0;
  DFS(1);
  printf("%d\n", ans);
}
int main() {
  init();
  play();
  return 0;
}
