#include <bits/stdc++.h>
using namespace std;
int main() {
  int p, i, j;
  scanf("%d", &p);
  int n = 1 << (p + 1);
  vector<int> a(n);
  for (i = 2; i < n; i++) scanf("%d", &a[i]);
  vector<int> up(n), mx(n);
  for (i = 1; i < n; i++) {
    int sum = 0;
    for (j = i; j > 1; j /= 2) sum += a[j];
    up[i] = sum;
  }
  int maxVal = *max_element(up.begin(), up.end());
  for (i = n / 2; i < n; i++) {
    mx[i] += maxVal - up[i];
  }
  for (i = n - 1; i > 1; i--) {
    int c = min(mx[i], mx[i ^ 1]);
    mx[i] -= c;
    mx[i ^ 1] -= c;
    mx[i / 2] += c;
  }
  printf("%d\n", accumulate(mx.begin(), mx.end(), 0));
}
