#include <bits/stdc++.h>
using namespace std;
int inp[10000], dp2[10000];
int max_light = 0, n, total, last_row;
int process2(int idx) {
  int &ret = dp2[idx];
  if (ret != -1) return ret;
  if (idx >= last_row) return ret = inp[idx];
  ret = max(process2(2 * idx + 1), process2(2 * idx + 2)) + inp[idx];
  return ret;
}
int process(int idx, int lights) {
  if (idx >= last_row) return max_light - dp2[idx] - lights;
  int temp =
      process(2 * idx + 1,
              lights + (max_light - dp2[idx] - lights) + inp[idx]) +
      process(2 * idx + 2, lights + (max_light - dp2[idx] - lights) + inp[idx]);
  return temp + max_light - dp2[idx] - lights;
}
int main() {
  scanf("%d", &n);
  total = pow(2, n + 1) - 2;
  last_row = pow(2, n) - 1;
  for (int i = 1; i <= total; i++) {
    scanf("%d", &inp[i]);
  }
  for (int i = last_row; i <= total; i++) {
    int idx = i, sum = 0;
    while (idx > 0) {
      sum += inp[idx];
      idx = (int)ceil((double)idx / (double)2) - 1;
    }
    max_light = max(max_light, sum);
  }
  memset(dp2, -1, sizeof(dp2));
  int a = process2(1);
  int b = process2(2);
  int ans = process(1, 0) + process(2, 0);
  cout << ans << endl;
  return 0;
}
