#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0, 0, 0, 0, 0};
int dy[] = {0, 0, 1, -1, 0, 0, 0, 0};
const int inf = 1000000000;
const double eps = 0.000000009;
int main() {
  int n;
  cin >> n;
  vector<vector<int> > a;
  for (int i = 1; i <= n; i++) {
    vector<int> x;
    for (int j = 0; j < pow(2, i); j++) {
      int xx;
      cin >> xx;
      x.push_back(xx);
    }
    a.push_back(x);
  }
  int ans = 0;
  for (int i = a.size() - 1; i >= 0; i--) {
    vector<int> x = a[i];
    for (int j = 0; j < x.size(); j += 2) {
      ans += abs(x[j] - x[j + 1]);
      if (i > 0) a[i - 1][j / 2] += max(x[j], x[j + 1]);
    }
  }
  cout << ans << endl;
  return 0;
}
