#include <bits/stdc++.h>
using namespace std;
long long ans, cr[40002], ar[40005], br[40005], nd[40005];
void build(int id, int l, int r) {
  if (l == r) {
    nd[id] = cr[l];
    return;
  }
  int md = (l + r) >> 1;
  build(id * 2, l, md);
  build(id * 2 + 1, md + 1, r);
  nd[id] = min(nd[2 * id], nd[2 * id + 1]);
  ans -= nd[id];
}
int main() {
  long long i, j, k, n, m, x, mx;
  cin >> n;
  queue<long long> Q;
  m = (1 << (n + 1)) - 2;
  Q.push(2);
  Q.push(3);
  mx = 0;
  k = 0;
  for (i = 1; i <= m; i++) {
    cin >> x;
    long long tp = Q.front();
    Q.pop();
    if (tp >= (1 << (n))) cr[k++] = x;
    ar[tp] = x;
    br[tp] = x + br[tp / 2];
    mx = max(mx, br[tp]);
    Q.push(2 * tp);
    Q.push(2 * tp + 1);
  }
  ans = 0;
  int p = (1 << n);
  k = 1;
  while (1) {
    cr[k] = mx - br[p];
    ans += cr[k];
    k++;
    p++;
    if (p > (m + 1)) break;
  }
  build(1, 1, (m + 2));
  cout << ans << endl;
  return 0;
}
