#include <bits/stdc++.h>
using namespace std;
int* ar;
int maxt = 0;
int loc = 0;
int sum = 2;
int re = 0;
int calmax(int index) {
  if (ar[index] == -1) return 0;
  return ar[index] + calmax(index / 2);
}
int dfs(int index) {
  if (index * 2 + 1 > sum) return maxt - calmax(index);
  int l = dfs(index * 2);
  int g = dfs(index * 2 + 1);
  if (l > g) {
    re += l - g;
    return g;
  } else {
    re += g - l;
    return l;
  }
  return max(l, g);
}
int main() {
  int n;
  cin >> n;
  ;
  for (int i = 0; i < n; i++) {
    sum *= 2;
  }
  ar = new int[sum];
  ar[0] = -1;
  ar[1] = -1;
  for (int i = 2; i < sum; i++) {
    cin >> ar[i];
  }
  int u = n / 2;
  int j = sum - 1;
  while (j >= u) {
    int y = calmax(j);
    if (y > maxt) {
      maxt = y;
    }
    j--;
  }
  dfs(1);
  cout << re;
}
