#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2345;
int n;
int roads[MAX_N];
int verteces[MAX_N];
int main() {
  cin >> n;
  int maxvert = pow(2, n + 1) - 1;
  for (int i = 0; i < maxvert - 1; i++) {
    int temproad;
    cin >> temproad;
    roads[i + 2] = temproad;
  }
  int ans = 0;
  for (int i = maxvert; i > 0; i--) {
    if (i * 2 <= maxvert) {
      int firstone = verteces[2 * i] + roads[2 * i];
      int secondone = verteces[2 * i + 1] + roads[2 * i + 1];
      ans += abs(firstone - secondone);
      verteces[i] = max(firstone, secondone);
    }
  }
  cout << ans << endl;
}
