#include <bits/stdc++.h>
using namespace std;
int tree_lv(int l) {
  int ret = 1;
  for (int i = 1; i <= l; i++) ret *= 2;
  return ret;
}
int main() {
  int n;
  cin >> n;
  vector<int> a(tree_lv(n + 1) + 10, 0);
  for (int i = 2; i < tree_lv(n + 1); i++) cin >> a[i];
  vector<int> tree(tree_lv(n + 1) + 10, 0);
  int ans = 0;
  for (int i = tree_lv(n) - 1; i >= 1; i--) {
    int f1 = tree[i * 2] + a[i * 2];
    int f2 = tree[2 * i + 1] + a[2 * i + 1];
    ans += abs(f1 - f2);
    tree[i] = max(f1, f2);
  }
  cout << ans;
  return 0;
}
