#include <bits/stdc++.h>
using namespace std;
int fun(int k, int num, int *arr) {
  int m, n, v1, v2;
  if (k > num)
    return 0;
  else {
    m = arr[k] + fun(2 * k + 2, num, arr);
    n = arr[k] + fun(2 * k + 3, num, arr);
  }
  return max(m, n);
}
int main() {
  int n;
  cin >> n;
  int minn = 0, v1, v2, k, num = pow(2, n + 1) - 3, arr[num + 1];
  for (k = 0; k <= num; k++) cin >> arr[k];
  for (k = 0; k <= num; k += 2) {
    v1 = fun(k, num, arr);
    v2 = fun(k + 1, num, arr);
    minn += max(v1, v2) - min(v1, v2);
  }
  cout << minn;
}
