#include <bits/stdc++.h>
using namespace std;
int light[3000];
int agr[3000];
int main() {
  int n;
  scanf("%d", &n);
  int m = pow(2, n + 1) - 1;
  for (int i = 2; i <= m; i++) {
    scanf("%d", &light[i]);
  }
  int ans = 0;
  for (int i = m; i >= 3; i--) {
    if (i % 2 == 0) continue;
    int a = light[i] + agr[i];
    int b = light[i - 1] + agr[i - 1];
    int c = a;
    if (a != b) {
      c = max(a, b);
      ans += abs(b - a);
    }
    agr[i / 2] = c;
    agr[(i - 1) / 2] = c;
  }
  printf("%d\n", ans);
  return 0;
}
