#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int m = 1 << (n + 1);
  vector<int> a(m - 1);
  for (int i = 0; i < (int)m - 2; ++i) cin >> a[i + 1];
  int rval = 0;
  for (int i = m / 2 - 1; i >= 1; --i) {
    int x = a[i * 2 - 1];
    int y = a[i * 2];
    int z = max(x, y);
    rval += z * 2 - x - y;
    a[i - 1] += z;
  }
  cout << rval << endl;
}
