#include <bits/stdc++.h>
using namespace std;
long long int val[6000], val1[6000];
int main() {
  long long int n, i, j, mx = 0;
  long long int ans = 0;
  scanf("%lld", &n);
  ;
  for (i = 2; i < (1 << (n + 1)); i++) {
    long long int x;
    scanf("%lld", &x);
    ;
    val[i] = x;
    val1[i] = x;
    mx = max(mx, x);
  }
  for (i = (1 << n) - 1; i > 1; i--) {
    val1[i] = max(val1[2 * i], val1[2 * i + 1]) + val[i];
    mx = max(mx, val1[i]);
  }
  cerr << "mx"
       << ": " << mx << endl;
  ;
  for (i = 2; i < 1 << (n + 1); i++) {
    int j = i / 2, up = 0;
    while (j > 1) {
      up += val[j];
      j /= 2;
    }
    cerr << "i"
         << ": " << i << " | "
         << "val1[i]"
         << ": " << val1[i] << " | "
         << "up"
         << ": " << up << endl;
    ;
    ans += mx - val1[i] - up;
    val[i] += mx - val1[i] - up;
  }
  cout << ans << endl;
  return 0;
}
