#include <bits/stdc++.h>
using namespace std;
int n;
int a[11111];
int main() {
  scanf("%d", &n);
  int m = (1 << (n + 1)) - 1;
  for (int i = 2; i <= m; i++) scanf("%d", &a[i]);
  int ans = 0;
  for (int i = m; i >= 2; i--)
    if (i % 2) {
      int add = max(a[i], a[i - 1]) - min(a[i], a[i - 1]);
      ans += add;
      a[i / 2] += max(a[i], a[i - 1]);
    }
  printf("%d\n", ans);
  return 0;
}
