#include <bits/stdc++.h>
using namespace std;
int a[2500];
int main() {
  int n, i, j, t1, t2, pow, ans;
  cin >> n;
  for (i = 1; i <= (1 << (n + 1)) - 2; i++) {
    cin >> a[i];
  }
  ans = 0;
  pow = n;
  for (i = (1 << (n + 1)) - 2 - (1 << n) + 1; i > 0; i -= (1 << (pow))) {
    for (j = i; j < i + (1 << pow); j += 2) {
      t1 = max(a[j], a[j + 1]);
      t2 = min(a[j], a[j + 1]);
      ans += (t1 - t2);
      a[j / 2] += t1;
    }
    pow--;
  }
  cout << ans << endl;
  return 0;
}
