#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main() {
  int n, val;
  scanf("%d", &n);
  for (int i = 0; i < ((1 << (n + 1)) - 2); i++) {
    scanf("%d", &val);
    v.push_back(val);
  }
  int curmax, curmin;
  int out = 0;
  while (n) {
    for (int i = (1 << (n + 1)) - 3; i >= (1 << n) - 2; i -= 2) {
      curmax = max(v[i], v[i - 1]);
      curmin = min(v[i], v[i - 1]);
      out += curmax - curmin;
      if (i / 2 - 1 >= 0) v[i / 2 - 1] += curmax;
    }
    n--;
  }
  cout << out;
  return 0;
}
