#include <bits/stdc++.h>
using namespace std;
const int maxN = 1 << 12;
int a[maxN];
long long ans;
int n;
long long dfs(int x = 1) {
  if (x > (1 << (n + 1))) return 0;
  long long left, right;
  left = dfs(x * 2);
  right = dfs(x * 2 + 1);
  left += a[x * 2];
  right += a[x * 2 + 1];
  ans += abs(right - left);
  return max(left, right);
}
int main(void) {
  scanf("%d", &n);
  for (int i = 2; i <= (1 << (n + 1)) - 1; i++) {
    scanf("%d", &a[i]);
  }
  ans = 0;
  dfs();
  printf("%lld\n", ans);
  return 0;
}
