#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const long long INFLL = 1000000000000000000LL;
int n;
int wyn;
int ile[10000];
int pot(int a) {
  int w = 1;
  while (a > 0) {
    w *= 2;
    a--;
  }
  return w;
}
int main() {
  scanf("%d", &n);
  int r = pot(n + 1) - 2;
  for (int i = 0; i < r; i++) {
    scanf("%d", &ile[i + 2]);
  }
  for (int i = r + 1; i >= 3; i -= 2) {
    if (ile[i] != ile[i - 1]) {
      int p = abs(ile[i] - ile[i - 1]);
      wyn += p;
    }
    ile[i / 2] += max(ile[i], ile[i - 1]);
  }
  printf("%d\n", wyn);
  return 0;
}
