#include <bits/stdc++.h>
using namespace std;
int n;
int arr[4000];
int total = 0;
int fix(int k) {
  if (k >= (1 << n)) {
    return 0;
  }
  int lc = 2 * k, rc = 2 * k + 1;
  int lcp = fix(lc);
  int rcp = fix(rc);
  int a = arr[lc] + lcp;
  int b = arr[rc] + rcp;
  total += (max(a, b) - min(a, b));
  return max(a, b);
}
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  int size = (1 << (n + 1)) - 2;
  for (int i = 0; i < (size); ++i) {
    cin >> arr[i + 2];
  }
  fix(1);
  cout << total;
}
