#include <bits/stdc++.h>
using namespace std;
long long mx = 0, p;
long long a[100000];
long long dfs(long long n) {
  if (2 * n > p) return a[n];
  int left = dfs(2 * n);
  int right = dfs(2 * n + 1);
  mx += abs(left - right);
  return a[n] += max(left, right);
}
int main() {
  long long k;
  cin >> k;
  p = pow(2, k + 1) - 2;
  for (int i = 1; i <= p; i++) {
    cin >> a[i + 1];
  }
  dfs(1);
  cout << mx;
}
