#include <bits/stdc++.h>
using namespace std;
int a[100000];
int cnt[100000];
int main() {
  int n;
  cin >> n;
  int k = (1 << (n + 1)) - 1;
  for (int i = 2; i <= k; ++i) cin >> a[i];
  int ans = 0;
  for (int i = k; i >= 1; --i) {
    if (2 * i + 1 > k) {
      cnt[i] = 0;
      continue;
    }
    ans += abs(cnt[2 * i] + a[2 * i] - cnt[2 * i + 1] - a[2 * i + 1]);
    cnt[i] = max(cnt[2 * i] + a[2 * i], cnt[2 * i + 1] + a[2 * i + 1]);
  }
  cout << ans << endl;
}
