#include <bits/stdc++.h>
using namespace std;
set<int> ww, hh;
multiset<int, greater<int> > lenw, lenh;
set<int>::iterator it1, it2, it;
long long g[10000];
map<long long, int> mp;
int a[10000];
int ans;
int dfs(int i) {
  if (a[i << 1] == -1 && a[i << 1 | 1] == -1) {
    return a[i];
  }
  int left = dfs(i << 1);
  int right = dfs(i << 1 | 1);
  ans += abs(left - right);
  return a[i] + max(left, right);
}
int main() {
  int n;
  scanf("%d", &n);
  memset(a, -1, sizeof(a));
  ans = 0;
  a[1] = 0;
  for (int i = 2; i <= (1 << (n + 1)) - 1; i++) {
    scanf("%d", a + i);
  }
  int temp = dfs(1);
  printf("%d\n", ans);
  return 0;
}
