#include <bits/stdc++.h>
using namespace std;
int n, x, k, a, ans = 0;
vector<int> vi;
vector<vector<pair<int, int> > > v(100000);
long long powr(int a, int b) {
  long long z = 1;
  while (b--) {
    z *= a;
  }
  return z;
}
int dfs(int node) {
  if (v[node].size() == 0) {
    return 0;
  }
  vector<int> l;
  for (int i = 0; i < v[node].size(); i++) {
    int child = v[node][i].first, cost = v[node][i].second;
    cost += dfs(child);
    l.push_back(cost);
  }
  sort(l.begin(), l.end());
  ans += l[1] - l[0];
  return l[1];
}
int main() {
  cin >> n;
  x = powr(2, n + 1) - 1;
  k = powr(2, n + 1) - 2;
  for (int i = 2; i <= x; i++) {
    int par = i / 2;
    cin >> a;
    v[par].push_back({i, a});
  }
  dfs(1);
  cout << ans << endl;
  return 0;
}
