#include <bits/stdc++.h>
using namespace std;
int d;
int n;
long long a[32768];
long long solve(int x) {
  if (x > n) return 0;
  long long res = solve(x * 2 + 1) + solve(x * 2 + 2);
  long long add = abs(a[x * 2 + 1] - a[x * 2 + 2]);
  a[x * 2 + 1] = a[x * 2 + 2] = max(a[x * 2 + 1], a[x * 2 + 2]);
  a[x] += a[x * 2 + 1];
  return res + add;
}
int main() {
  scanf("%d", &d);
  n = 2;
  for (int i = 0; i < d; i++) n <<= 1;
  n -= 2;
  for (int i = 1; i <= n; i++) scanf("%lld", a + i);
  printf(
      "%lld"
      "\n",
      solve(0));
  return 0;
}
