#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
  int n;
  cin >> n;
  int ans = 0;
  int count = (1 << (n + 1)) - 2;
  vector<int> lights(count);
  for (int i = 0; i < count; i++) {
    cin >> lights[i];
  }
  for (int j = count - 1; j >= 1; j -= 2) {
    ans += abs(lights[j] - lights[j - 1]);
    if (j > 1) lights[j / 2 - 1] += max(lights[j], lights[j - 1]);
  }
  cout << ans << endl;
  return 0;
}
