#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j, res = 0, a[12345];
  cin >> n;
  int p = 1;
  for (i = 0; i <= n; i++) p *= 2;
  p -= 2;
  for (i = 2; i <= p + 1; i++) cin >> a[i];
  for (i = p + 1; i > 1; i--)
    if (i / 2 == (i - 1) / 2) {
      res += abs(a[i] - a[i - 1]);
      a[i / 2] += max(a[i], a[i - 1]);
    }
  cout << res;
}
