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