#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans = 0;
  cin >> n;
  n = (1 << (n + 1)) - 1;
  int a[n + 5];
  for (int i = 2; i <= n; i++) cin >> a[i];
  for (; n > 1; n -= 2) {
    a[n / 2] += max(a[n], a[n - 1]);
    ans += abs(a[n] - a[n - 1]);
  }
  cout << ans << endl;
  return 0;
}
