#include <bits/stdc++.h>
using namespace std;
vector<int> v[3001];
int viz[3001], d[3001], cost[3001], a[3001], n, maxim;
void dfs(int nod) {
  viz[nod] = 1;
  for (vector<int>::iterator it = v[nod].begin(); it != v[nod].end(); it++)
    if (viz[*it] == 0) {
      d[*it] = d[nod] + cost[*it - 1];
      dfs(*it);
    }
}
void dfs2(int nod) {
  if (nod >= (1 << n)) {
    a[nod] = maxim - d[nod];
    return;
  }
  dfs2(nod * 2);
  dfs2(nod * 2 + 1);
  int m = min(a[nod * 2], a[nod * 2 + 1]);
  a[nod * 2] -= m;
  a[nod * 2 + 1] -= m;
  a[nod] = m;
}
int main() {
  int i, r, x, j, ok, sol;
  cin >> n;
  for (i = 1; i <= (1 << (n + 1)) - 1; i++) cin >> cost[i];
  for (i = 1; i <= (1 << n) - 1; i++) {
    v[i].push_back(2 * i);
    v[i].push_back(2 * i + 1);
  }
  dfs(1);
  maxim = -1;
  for (i = (1 << n); i <= (1 << (n + 1)) - 1; i++) maxim = max(maxim, d[i]);
  dfs2(1);
  sol = 0;
  for (i = 2; i <= (1 << (n + 1)) - 1; i++) sol = sol + a[i];
  cout << sol;
  return 0;
}
