#include <bits/stdc++.h>
using namespace std;
int max_[10000], sum[10000], mmax;
int n, m, cnt = 0;
void dfs(int pos, int add) {
  if (pos * 2 > m) return;
  cnt += 2 * mmax - max_[pos * 2] - max_[pos * 2 + 1] - 2 * add;
  dfs(pos * 2, mmax - max_[pos * 2]);
  dfs(pos * 2 + 1, mmax - max_[pos * 2 + 1]);
}
int main() {
  cin >> n;
  m = (1 << (n + 1)) - 1;
  sum[1] = 0;
  for (int i = 2; i <= m; i++) {
    int a;
    cin >> a;
    sum[i] = sum[i / 2] + a;
    if (i >= (1 << n)) max_[i] = sum[i];
  }
  for (int i = m; i >= 1; i--) max_[i / 2] = max(max_[i], max_[i / 2]);
  mmax = max_[1];
  dfs(1, 0);
  cout << cnt;
  return 0;
}
