#include <bits/stdc++.h>
using namespace std;
int tree[10000][2];
int lights;
void mark(int node, int l) {
  int p = node / 2;
  if (p == 0) return;
  tree[p][node % 2] = min(tree[p][node % 2], lights - l);
  mark(p, l);
}
int cont[10000];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  memset(tree, -1, sizeof(tree));
  int n, a, p;
  cin >> n;
  int lim = (1 << (n + 1)) - 1;
  for (int i = 2; i <= lim; ++i) {
    cin >> a;
    p = i / 2;
    tree[p][i % 2] = a;
  }
  lights = 0;
  queue<int> Q;
  Q.push(1);
  Q.push(0);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    int l = Q.front();
    Q.pop();
    if (tree[u][0] == -1) {
      lights = max(lights, l);
      cont[u] = l;
      continue;
    }
    Q.push(2 * u);
    Q.push(tree[u][0] + l);
    Q.push(2 * u + 1);
    Q.push(tree[u][1] + l);
    tree[u][0] = tree[u][1] = 0x3fffffff;
  }
  for (int i = 1 << n; i <= lim; ++i) mark(i, cont[i]);
  int ans = 0;
  Q.push(1);
  Q.push(0);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    int l = Q.front();
    Q.pop();
    if (tree[u][0] == -1) continue;
    ans += tree[u][0] + tree[u][1] - 2 * l;
    Q.push(2 * u);
    Q.push(tree[u][0]);
    Q.push(2 * u + 1);
    Q.push(tree[u][1]);
  }
  cout << ans;
}
