#include <bits/stdc++.h>
using namespace std;
int arr[1000001];
vector<pair<int, int> > v[10000];
int ma = 0, ma1 = 0, ma2 = 0;
int ans = 0;
int dfs(int x, int co) {
  if (v[x].size() == 0) return co;
  vector<pair<int, int> >::iterator it = v[x].begin();
  vector<pair<int, int> >::iterator it1 = it;
  it++;
  int a = dfs(2 * x, it1->second);
  int b = dfs(2 * x + 1, it->second);
  ans += abs(a - b);
  return max(a, b) + co;
}
int main() {
  int n, i, j;
  cin >> n;
  int x = pow(2, n + 1) - 2;
  for (i = 0; i < x; i++) cin >> arr[i];
  queue<int> q;
  q.push(1);
  i = 0;
  while (i != x) {
    int x = q.front();
    q.pop();
    v[x].push_back(make_pair(2 * x, arr[i]));
    v[x].push_back(make_pair(2 * x + 1, arr[i + 1]));
    i += 2;
    q.push(2 * x);
    q.push(2 * x + 1);
  }
  dfs(1, 0);
  cout << ans;
}
