#include <bits/stdc++.h>
using namespace std;
inline void inp(long long *n) {
  *n = 0;
  long long ch = getchar();
  int sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') sign = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    *n = (*n << 3) + (*n << 1) + ch - '0', ch = getchar();
  *n = *n * sign;
}
inline void fastp(long long a) {
  register char c;
  char snum[20];
  int i = 0;
  do {
    snum[i++] = a % 10 + 48;
    a = a / 10;
  } while (a != 0);
  i = i - 1;
  while (i >= 0) putchar(snum[i--]);
  putchar('\n');
}
long long arr[2500];
pair<long long, long long> solve(long long node, long long n) {
  if (node > pow(2, n + 1) - 2) {
    pair<long long, long long> m;
    m.first = m.second = 0;
    return m;
  }
  if (node > pow(2, n) - 2) {
    pair<long long, long long> m;
    m.first = arr[node];
    m.second = 0;
    return m;
  }
  pair<long long, long long> a = solve(2 * node + 1, n);
  pair<long long, long long> b = solve(2 * node + 2, n);
  pair<long long, long long> res;
  res.second += abs(a.first - b.first) + a.second + b.second;
  res.first = max(a.first, b.first) + arr[node];
  return res;
}
int main() {
  long long t, n, m = 0, i, j, l, r;
  cin >> n;
  l = pow(2, n + 1) - 2;
  for (i = 1; i <= l; i++) {
    cin >> arr[i];
    m += arr[i];
  }
  pair<long long, long long> res = solve(0, n);
  fastp(res.second);
  return 0;
}
