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