#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
int a[300003];
int main() {
  if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  for (int i = 2; i < (1 << (n + 1)); ++i) cin >> a[i];
  ll ans = 0;
  for (int i = (1 << n) - 1; i > 0; --i) {
    ans += abs(a[i << 1] - a[i << 1 | 1]);
    a[i] += max(a[i << 1], a[i << 1 | 1]);
  }
  cout << ans;
}
