#include <bits/stdc++.h>
using namespace std;
int a[3000];
int main() {
  int n;
  cin >> n;
  int tot = (1 << n + 1) - 1;
  for (int i = 2; i <= tot; ++i) cin >> a[i];
  int pw = 1 << n;
  int ans = 0;
  tot /= 2;
  for (int i = tot; i >= 1; --i) {
    int mx = max(a[2 * i], a[2 * i + 1]);
    ans += mx - a[2 * i] + mx - a[2 * i + 1];
    a[i] += mx;
  }
  cout << ans;
  return 0;
}
