#include <bits/stdc++.h>
using namespace std;
int need = 0;
int n;
int a[1 << 12];
int R(int w, int s) {
  if (w >= (1 << n) - 1) {
    return s;
  }
  int ret = 0;
  int x = R(2 * w + 1, s + a[2 * w + 1]);
  int y = R(2 * w + 2, s + a[2 * w + 2]);
  ret = max(x, y);
  need += max(x, y) - min(x, y);
  return ret;
}
int main() {
  cin >> n;
  for (int i = 0; i < (1 << (n + 1)) - 2; i++) {
    cin >> a[i + 1];
  }
  int r = R(0, 0);
  cout << need;
}
