#include <bits/stdc++.h>
int a[20000];
int b[20000];
int main() {
  int n;
  scanf("%d", &n);
  int sum = 0;
  for (int i = 2; i < (1 << (n + 1)); i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  for (int i = (1 << (n + 1)) - 1; i; i--) {
    b[i] = a[i] + ((b[i * 2]) > (b[i * 2 + 1]) ? (b[i * 2]) : (b[i * 2 + 1]));
    a[i] = a[i] +
           ((b[i * 2] - b[i * 2 + 1]) > 0 ? (b[i * 2] - b[i * 2 + 1])
                                          : (-(b[i * 2] - b[i * 2 + 1]))) +
           a[i * 2] + a[i * 2 + 1];
  }
  printf("%d", a[1] - sum);
}
