#include <bits/stdc++.h>
using namespace std;
bool solve() {
  int n;
  cin >> n;
  vector<int> a(1 << (n + 1));
  vector<int> s(1 << (n + 1));
  for (int i = 2; i < (1 << (n + 1)); ++i) cin >> a[i];
  long long res = 0;
  for (int i = (1 << n) - 1; i >= 1; --i) {
    s[i] = max(a[i * 2] + s[i * 2], a[i * 2 + 1] + s[i * 2 + 1]);
    res += max(a[i * 2] + s[i * 2], a[i * 2 + 1] + s[i * 2 + 1]) -
           min(a[i * 2] + s[i * 2], a[i * 2 + 1] + s[i * 2 + 1]);
  }
  cout << res << endl;
  return false;
}
int main() {
  while (solve())
    ;
  return 0;
}
