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