#include <bits/stdc++.h>
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return ((a * b) / gcd(a, b)); }
using namespace std;
int main() {
  int a, b, c, i, j, k, tc, t;
  int n, m, cnt = 0;
  int ara[100001];
  scanf("%d", &n);
  m = (2 << n) - 1;
  for (i = 2; i <= m; i++) scanf("%d", &ara[i]);
  for (i = m; i > 1; i -= 2) {
    ara[i / 2] += ara[i] > ara[i - 1] ? ara[i] : ara[i - 1];
    cnt += abs(ara[i] - ara[i - 1]);
  }
  printf("%d\n", cnt);
  return 0;
}
