#include <bits/stdc++.h>
using namespace std;
int a[12345];
int main() {
  ios::sync_with_stdio(false);
  int n, i, left, right;
  cin >> n;
  for (i = 2; i <= (1 << (n + 1)) - 1; i++) cin >> a[i];
  long long ans = 0;
  for (i = (1 << n) - 1; i >= 1; i--) {
    left = a[i * 2];
    right = a[i * 2 + 1];
    ans += abs(left - right);
    a[i] += max(left, right);
  }
  cout << ans << endl;
  return 0;
}
