#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t, n, num, m, k;
  cin >> n;
  long long st = (1ll << n) - 1;
  n = (1ll << (n + 1)) - 2;
  long long aa[n + 3];
  for (long long i = 2; i < n + 2; i++) {
    cin >> aa[i];
  }
  long long ret = 0;
  for (long long i = st; i >= 1; i--) {
    ret += abs(aa[(2ll * i) + 1] - aa[(2ll * i)]);
    aa[i] += min(aa[(2ll * i) + 1], aa[(2ll * i)]) +
             abs(aa[(2ll * i) + 1] - aa[(2ll * i)]);
  }
  cout << ret;
  return 0;
}
