#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  vector<int> pl;
  vector<int> sum;
  int a;
  int otv = 0;
  cin >> n;
  int m = pow(2, n + 1) - 2;
  pl.push_back(0);
  for (int i = 1; i <= m; ++i) {
    cin >> a;
    pl.push_back(a);
  }
  for (int j = n; j > 0; j--)
    for (int i = pow(2, j); i < pow(2, j + 1) - 1; i += 2) {
      pl[i / 2 - 1] += max(pl[i - 1], pl[i]);
      otv += abs(pl[i - 1] - pl[i]);
    }
  cout << otv;
  return 0;
}
