#include <bits/stdc++.h>
using namespace std;
vector<int> maxval;
vector<int> val;
int valnum, maxnum;
int ans = 0;
int cr_mval(int ind) {
  if (ind >= maxnum) return maxval[ind] = 0;
  int t1 = cr_mval(2 * ind);
  int t2 = cr_mval(2 * ind + 1);
  maxval[ind] = max(t1 + val[2 * ind], t2 + val[2 * ind + 1]);
  return maxval[ind];
}
void calcans(int ind) {
  if (ind >= maxnum) return;
  ans += abs(maxval[2 * ind] + val[2 * ind] - maxval[2 * ind + 1] -
             val[2 * ind + 1]);
  calcans(2 * ind);
  calcans(2 * ind + 1);
}
int main() {
  int n;
  cin >> n;
  maxnum = pow(2, n);
  valnum = 2 * maxnum;
  val.push_back(-2);
  val.push_back(-2);
  for (int i = 0; i < valnum - 2; i++) {
    int temp;
    cin >> temp;
    val.push_back(temp);
  }
  for (int i = 0; i < 2 * maxnum; i++) {
    maxval.push_back(-2);
  }
  cr_mval(1);
  calcans(1);
  cout << ans;
}
