#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  scanf("%d ", &n);
  n++;
  vector<int> a(1 << n);
  for (int i = 2; i < (1 << n); i++) {
    scanf("%d\n", &a[i]);
  }
  vector<int> mx_left(1 << n), mx_right(1 << n);
  int m = 1 << n;
  for (int i = m - 1; i > 0; i--) {
    if (i >= (1 << (n - 1))) {
      mx_left[i] = 0;
      mx_right[i] = 0;
    } else {
      int left = 2 * i;
      int right = 2 * i + 1;
      mx_left[i] = a[left] + max(mx_left[left], mx_right[left]);
      mx_right[i] = a[right] + max(mx_left[right], mx_right[right]);
    }
  }
  int allmx = max(mx_left[1], mx_right[1]), r = 0;
  vector<int> add(m);
  for (int i = 1; i < m / 2; i++) {
    if (mx_left[i] + add[i] < allmx) {
      int d = allmx - (mx_left[i] + add[i]);
      r += d;
      add[2 * i] = d;
    }
    add[2 * i] += add[i] + a[2 * i];
    if (mx_right[i] + add[i] < allmx) {
      int d = allmx - (mx_right[i] + add[i]);
      r += d;
      add[2 * i + 1] = d;
    }
    add[2 * i + 1] += add[i] + a[2 * i + 1];
  }
  printf("%d\n", r);
}
int main() {
  solve();
  return 0;
}
