#include <bits/stdc++.h>
using namespace std;
int t[1 << 11], n;
pair<int, int> solve(int i) {
  if (i >= (1 << n)) return {t[i], 0};
  pair<int, int> a = solve(i << 1), b = solve(i << 1 | 1);
  return {max(a.first, b.first) + t[i],
          a.second + b.second + abs(a.first - b.first)};
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 2; i < 1 << (n + 1); i++) {
    cin >> t[i];
  }
  cout << solve(1).second << '\n';
  return 0;
}
