#include <bits/stdc++.h>
int road[10000];
int MAX(int a, int b) { return a > b ? a : b; }
int main() {
  memset(road, 0, sizeof(road));
  int n;
  scanf("%d", &n);
  int i, j;
  int Max = 1;
  for (i = 0; i <= n; i++) Max *= 2;
  Max--;
  for (i = 2; i <= Max; i++) {
    scanf("%d", road + i);
  }
  int sum = 0;
  for (i = Max / 2; i >= 1; i--) {
    int max = MAX(road[i * 2], road[i * 2 + 1]);
    road[i] = max + road[i];
    sum += max - road[i * 2] + max - road[i * 2 + 1];
  }
  printf("%d\n", sum);
}
