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