#include <bits/stdc++.h>
using namespace std;
int res, mx, ar[50000];
void dfs(int i, int cnt) {
  if (ar[i] == 0) return;
  int diff = (mx - (ar[i] + cnt));
  res += diff;
  dfs(2 * i, cnt + diff), dfs(2 * i + 1, cnt + diff);
}
int main() {
  int n;
  cin >> n;
  for (int i = 2; i <= ((1 << (n + 1)) - 1); i++) cin >> ar[i];
  for (int i = 1 << n; i < (1 << (n + 1)); i++) {
    int j = i;
    int cnt = 0;
    while (j > 0) cnt += ar[j], j /= 2;
    ar[i] = cnt, mx = max(mx, cnt);
  }
  for (int i = 1 << n; i < (1 << (n + 1)); i++) {
    int j = i;
    while (j > 1) ar[j / 2] = max(ar[j / 2], ar[j]), j /= 2;
  }
  dfs(1, 0);
  cout << res;
}
