#include <bits/stdc++.h>
using namespace std;
long long int tot(int i, long long int a[]) {
  if (i < 2) return 0;
  return a[i] + tot(i >> 1, a);
}
long long int maxch(int i, int n, long long int a[]) {
  if (i > ((1 << (n + 1)) - 1)) return 0;
  return max(a[i << 1] + maxch(i << 1, n, a),
             a[(i << 1) + 1] + maxch((i << 1) + 1, n, a));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  long long int a[10000];
  for (int i = 0; i < ((1 << (n + 1)) - 2); i++) {
    cin >> a[i + 2];
  }
  long long int goal = -1;
  for (int i = ((1 << (n + 1)) - 1); i >= 2; i--) {
    long long int sum = 0;
    int act = i;
    do {
      sum += a[act];
      act = (act >> 1);
    } while (act >= 2);
    goal = max(goal, sum);
  }
  long long int c = 0;
  for (int i = 2; i <= ((1 << (n + 1)) - 1); i++) {
    long long int inc = goal - (maxch(i, n, a) + tot(i, a));
    c += inc;
    a[i] += inc;
  }
  cout << c << endl;
  return 0;
}
