#include <bits/stdc++.h>
using namespace std;
long lights[50000];
long lightToNode[50000];
int main() {
  long levels;
  cin >> levels;
  levels++;
  long nodes = (1 << (levels)) - 1;
  long streets = nodes - 1;
  for (long i = 1; i <= streets; i++) {
    cin >> lights[i + 1];
  }
  lights[1] = 0;
  lightToNode[0] = 0;
  for (long i = 1; i <= nodes; i++) {
    lightToNode[i] = lights[i] + lightToNode[i / 2];
  }
  long solution = 0;
  for (long sep = 1; sep <= nodes / 2; sep *= 2) {
    for (long init = (1 << (levels - 1)); init <= nodes; init += sep * 2) {
      long dif = labs(lightToNode[init] - lightToNode[init + sep]);
      solution += dif;
      lightToNode[init] = max(lightToNode[init], lightToNode[init + sep]);
    }
  }
  cout << solution << endl;
  return 0;
}
