#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
  long long ans = 1;
  for (long long i = 0; i < b; i++) ans *= a;
  return ans;
}
int a[10000], b[10000];
int main() {
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  for (long long i = 0; i < power(2, n + 1) - 2; i++) cin >> a[i + 2];
  long long ans = 0;
  for (long long i = n; i > 0; i--) {
    long long start = power(2, i), end = power(2, i + 1) - 1;
    for (long long j = start; j < end; j += 2) {
      if (a[j] + b[j] > a[j + 1] + b[j + 1])
        ans += a[j] + b[j] - b[j + 1] - a[j + 1],
            a[j + 1] = a[j] + b[j] - b[j + 1];
      else
        ans += a[j + 1] + b[j + 1] - b[j] - a[j],
            a[j] = a[j + 1] + b[j + 1] - b[j];
      b[j / 2] = a[j] + b[j];
    }
  }
  cout << ans;
  return 0;
}
