#include <bits/stdc++.h>
using namespace std;
int sz = (1 << 11), mx;
vector<int> a, p;
int go(int ind, int sum) {
  if (ind * 2 >= sz) return p[ind] = sum;
  return p[ind] = max(go(ind * 2, sum + a[ind * 2]),
                      go(ind * 2 + 1, sum + a[ind * 2 + 1]));
}
int get(int ind, int t) {
  if (ind * 2 >= sz) return mx - p[ind] - t;
  int ret = mx - p[ind] - t;
  return ret + get(ind * 2, t + ret) + get(ind * 2 + 1, t + ret);
}
int main() {
  int n;
  while (cin >> n) {
    a.resize((1 << (n + 1)));
    p.resize(a.size());
    sz = (1 << (n + 1));
    for (int i = 2; i < a.size(); cin >> a[i++])
      ;
    mx = go(1, 0);
    cout << get(1, 0) << endl;
  }
  return 0;
}
