#include <bits/stdc++.h>
int power(int n) {
  if (n == 0) return 1;
  int a = power(n / 2);
  if (n % 2 == 1) return a * a * 2;
  return a * a;
}
int ans = 0, a[5000];
void func(int n) {
  if (n == 0) return;
  if (n == 1) {
    ans += ((a[0] > a[1] ? a[0] : a[1]) - (a[0] > a[1] ? a[1] : a[0]));
    return;
  }
  int i, b = power(n);
  for (i = b - 2; i < 2 * b - 2; i += 2) {
    ans += ((a[i] > a[i + 1] ? a[i] : a[i + 1]) -
            (a[i] > a[i + 1] ? a[i + 1] : a[i]));
    if (i != 0) a[i / 2 - 1] += (a[i] > a[i + 1] ? a[i] : a[i + 1]);
  }
  func(n - 1);
}
int main() {
  int n, i;
  scanf("%d", &n);
  for (i = 0; i < power(n + 1) - 2; i++) scanf("%d", &a[i]);
  func(n);
  printf("%d\n", ans);
  return 0;
}
