#include <bits/stdc++.h>
using namespace std;
long long i, j, k, p, l, m, n, e, pas, mx, mn, kk, kkk,
    a[50000] = {0}, b[5000], i1, i2, j2, i3, i4, i5, i6, i7;
string s;
vector<long long> v;
queue<long long> q;
int go(int i) {
  if (i >= (1 << n))
    b[i] = a[i];
  else
    b[i] = max(go(i * 2), go(i * 2 + 1)) + a[i];
  return b[i];
}
void go_again(int i, int k) {
  if (i > (2 << n) - 1) return;
  pas += mx - b[i] - k;
  go_again(i * 2, a[i] + k + mx - b[i] - k);
  go_again(i * 2 + 1, a[i] + k + mx - b[i] - k);
}
int main() {
  cin >> n;
  for (i = 2; i <= (2 << n) - 1; i++) cin >> a[i];
  go(1);
  mx = 0;
  for (i = 1; i <= (2 << n) - 1; i++) {
    mx = max(mx, b[i]);
  }
  go_again(1, 0);
  cout << pas;
}
