#include <bits/stdc++.h>
using namespace std;
int n, o = 0;
int ans = 0;
int a[1000500], h[1000500], dp[1000500];
int get(int v) {
  int res = 0;
  while (v > 1) {
    res += a[v];
    v >>= 1;
    dp[v] = max(dp[v], res);
  }
  return res;
}
void dfs(int ver, int dec, int pl, int pr) {
  dp[ver] += dec;
  int add = o - dp[ver] - a[ver];
  ans += add;
  if (ver >= pl && ver <= pr) return;
  dfs(ver + ver, dec + add + a[ver], pl, pr);
  dfs(ver + ver + 1, dec + add + a[ver], pl, pr);
}
int main() {
  scanf("%d", &n);
  memset(dp, 0, sizeof(dp));
  int pn = n + 1;
  int m = (1 << pn) - 2;
  for (int i = 1; i <= m; i++) scanf("%d", &a[i + 1]);
  int le = (1 << n);
  int ri = le * 2 - 1;
  o = 0;
  ans = 0;
  for (int i = le; i <= ri; i++) {
    h[i] = get(i);
    o = max(o, h[i]);
  }
  dfs(2, 0, le, ri);
  dfs(3, 0, le, ri);
  printf("%d\n", ans);
  return 0;
}
