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