#include <bits/stdc++.h>
using namespace std;
int a[2105];
int n, k;
long long memo[2200];
long long count(int i) {
  if (2 * i > k) return a[i];
  long long &ret = memo[i];
  if (ret != -1) return ret;
  return ret = a[i] + max(count(2 * i), count(2 * i + 1));
}
long long f(int i) {
  if (2 * i > k) return 0;
  long long c = fabsl(count(2 * i) - count(2 * i + 1));
  return c + f(2 * i) + f(2 * i + 1);
}
int main() {
  cin >> n;
  k = (1LL << (n + 1)) - 1;
  memset(memo, -1, sizeof(memo));
  for (int i = 2; i <= k; i++) {
    cin >> a[i];
  }
  cout << f(1) << endl;
  return 0;
}
