#include <bits/stdc++.h>
using namespace std;
int a[5000], temp, lst, lgt[5005], maxm = 0;
int returnPow(int n) {
  int tmp = 1;
  for (int i = 1; i <= n; i++) tmp *= 2;
  return tmp;
}
void dfs(int node, int val) {
  if (2 * node > temp) {
    lgt[node] = val;
    return;
  }
  dfs(2 * node, val + a[2 * node]);
  dfs(2 * node + 1, val + a[2 * node + 1]);
  lgt[node] = max(lgt[2 * node], lgt[2 * node + 1]);
}
void update(int node, int val) {
  if (node > temp) return;
  lgt[node] += val;
  update(2 * node, val);
  update(2 * node + 1, val);
}
int main() {
  int i, j, n, m, ans = 0;
  cin >> n;
  temp = returnPow(n + 1);
  lst = returnPow(n);
  temp -= 1;
  for (i = 2; i <= temp; i++) cin >> a[i];
  dfs(1, 0);
  for (i = lst; i <= temp; i++) maxm = max(maxm, lgt[i]);
  for (i = 2; i <= temp; i++) {
    if (lgt[i] < maxm) {
      ans += (maxm - lgt[i]);
      update(i, maxm - lgt[i]);
    }
  }
  cout << ans;
  return 0;
}
