#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
int n, a[20000], sol[20000];
int proc(int v) {
  if (v >= (1 << n)) {
    sol[v] = 0;
    return 0;
  }
  int l = v + v, r = v + v + 1;
  int pl = proc(l);
  int pr = proc(r);
  int ret = pl + pr;
  sol[l] += a[l];
  sol[r] += a[r];
  if (sol[l] > sol[r]) swap(l, r);
  int dif = sol[r] - sol[l];
  ret += dif;
  sol[v] = sol[r];
  return ret;
}
int main() {
  while (scanf("%d", &n) == 1) {
    for (int(i) = (2); (i) < ((1 << (n + 1))); ++(i)) {
      scanf("%d", &a[i]);
    }
    printf("%d\n", proc(1));
  }
  return 0;
}
