#include <bits/stdc++.h>
using namespace std;
long n, cost[1 << 20], l, r, mx, ans, sub[1 << 20];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 2; i <= (1 << n) * 2 - 1; i++) {
    cin >> cost[i];
  }
  mx = 0;
  for (int i = (1 << n); i; i--) {
    l = cost[i * 2] + sub[i * 2];
    r = cost[i * 2 + 1] + sub[i * 2 + 1];
    ans += abs(l - r);
    sub[i] = max(l, r);
  }
  cout << ans << endl;
  cin.get();
  cin.get();
  return 0;
}
