#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  return (!b) ? a : gcd(b, a % b);
}
int n;
int a[5000];
int tot[5000];
int ad[5000];
void solve() {
  cin >> n;
  for (int i = 2; i <= ((1 << (n + 1)) - 1); i++) {
    cin >> a[i];
  }
  int maxc = 0;
  for (int i = (1 << (n)); i <= ((1 << (n + 1)) - 1); i++) {
    int c = 0;
    int tm = i;
    while (tm != 1) {
      c += a[tm];
      tm /= 2;
    }
    tot[i] = c;
    maxc = max(c, maxc);
  }
  int sum = 0;
  for (int i = ((1 << (n + 1)) - 1); i >= 1 << (n); i -= 1) {
    ad[i] = maxc - tot[i];
    sum += ad[i];
  }
  for (int i = ((1 << (n)) - 1); i >= 1; i -= 1) {
    sum -= min(ad[2 * i], ad[2 * i + 1]);
    ad[i] += min(ad[2 * i], ad[2 * i + 1]);
  }
  cout << sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  int t = 1;
  while (t--) {
    solve();
  }
}
