#include <bits/stdc++.h>
using namespace std;
int a[10500];
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    n = pow(2, n + 1);
    for (int i = 2; i < n; i++) cin >> a[i];
    int sum = 0;
    for (int i = n - 1; i >= 2; i -= 2) {
      if (a[i] > a[i - 1]) {
        sum += (a[i] - a[i - 1]);
        a[i / 2] += a[i];
      } else {
        sum += (a[i - 1] - a[i]);
        a[i / 2] += a[i - 1];
      }
    }
    cout << sum << endl;
  }
}
