#include <bits/stdc++.h>
using namespace std;
int N, tot, cc;
int a[3000];
int max(int a, int b) { return a > b ? a : b; }
int maxsub(int a[], int n) {
  if (n >= cc / 2) return max(a[n * 2], a[n * 2 + 1]) + a[n];
  return max(maxsub(a, 2 * n), maxsub(a, 2 * n + 1)) + a[n];
}
int ans(int a[], int n) {
  int i, j, s1 = 0, s2 = 0;
  if (n * 2 >= cc) return abs(a[n * 2 + 1] - a[n * 2]);
  s1 = maxsub(a, 2 * n);
  s2 = maxsub(a, 2 * n + 1);
  if (s1 < s2) {
    s1 = s2 - s1;
  } else {
    s1 = s1 - s2;
  }
  return ans(a, n * 2) + ans(a, n * 2 + 1) + s1;
}
int main() {
  int n, i, j, k, sum;
  cin >> N;
  n = N;
  tot = pow(2, n + 1) - 1;
  cc = pow(2, n);
  for (i = 2; i <= tot; i++) cin >> a[i];
  cout << ans(a, 1) << endl;
}
