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