#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long x = pow(2, n + 1) - 2;
  vector<long long> p(x + 1);
  for (long long i = 1; i <= x; i++) {
    cin >> p[i];
  }
  vector<long long> a(pow(2, n + 1) + 1, 0);
  long long c = 0;
  for (long long i = n; i >= 1; i--) {
    long long l = pow(2, i);
    long long h = l * 2 - 1;
    for (long long j = l; j <= h; j += 2) {
      p[j / 2 - 1] += max(p[j - 1], p[j]);
      c += max(p[j - 1], p[j]) - min(p[j - 1], p[j]);
    }
  }
  cout << c << endl;
  return 0;
}
