#include <bits/stdc++.h>
using namespace std;
int a[10000];
int main() {
  int n;
  scanf("%d", &n);
  int m = (double)pow(2, n + 1) - 1;
  for (int i = 2; i <= m; i++) scanf("%d", &a[i]);
  int cnt = 0;
  for (int i = m / 2; i >= 1; i--) {
    cnt += abs(a[2 * i] - a[2 * i + 1]);
    a[i] += max(a[2 * i], a[2 * i + 1]);
  }
  cout << cnt;
  return 0;
}
