#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, lastNode, fId = 1, total, output = 0;
  int length[4100], h[4100], sum[4100] = {0};
  cin >> n;
  lastNode = pow(2, n + 1) - 1;
  for (int i = 2; i <= lastNode; i++) cin >> length[i];
  h[1] = 0;
  for (int i = 1; i <= lastNode; i++) {
    if (h[i] > h[fId]) fId = i;
    h[i * 2] = h[i] + length[i * 2];
    h[i * 2 + 1] = h[i] + length[i * 2 + 1];
  }
  total = h[fId];
  for (int i = pow(2, n); i <= lastNode; i++) sum[i] = total - h[i];
  for (int i = n; i >= 0; i--) {
    int p = pow(2, i + 1) - 1;
    for (int j = (p + 1) / 2; j <= p; j += 2) {
      int temp = min(sum[j], sum[j ^ 1]);
      output += (sum[j] - temp) + (sum[j ^ 1] - temp);
      sum[j / 2] = temp;
    }
  }
  cout << output;
  return 0;
}
