#include <bits/stdc++.h>
using namespace std;
long long a[2200], m, s, n;
long long pth[2200];
long long nradd[2200], maxi;
void dfs(long long nod) {
  if (nod > (1 << (n + 1)) - 1) return;
  if ((1 << n) <= nod && nod < (1 << (n + 1))) {
    return;
  }
  dfs(nod * 2);
  dfs(nod * 2 + 1);
  nradd[nod] = min(nradd[nod * 2], nradd[nod * 2 + 1]);
  nradd[nod * 2] -= nradd[nod];
  nradd[nod * 2 + 1] -= nradd[nod];
}
int main() {
  cin >> n;
  for (long long i = 2; i < (1 << (n + 1)); i++) {
    cin >> a[i];
  }
  for (long long i = (1 << n); i < (1 << (n + 1)); i++) {
    long long ci = i;
    while (ci > 1) {
      pth[i] += a[ci];
      ci /= 2;
    }
    maxi = max(maxi, pth[i]);
  }
  for (long long i = (1 << n); i < (1 << (n + 1)); i++) {
    nradd[i] = maxi - pth[i];
  }
  dfs(1);
  for (long long i = 1; i < (1 << (n + 1)); i++) {
    s += nradd[i];
  }
  cout << s;
}
