#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T Max(T a, T b) {
  return a > b ? a : b;
}
template <typename T>
inline T Min(T a, T b) {
  return a < b ? a : b;
}
const double PI = acos(-1.0);
int N;
int a[3000], b[3000];
int ans;
void work() {
  for (int i = 2; i <= ((1 << (N + 1)) - 1); i++) {
    scanf("%d", &a[i]);
  }
  ans = 0;
  queue<int> Q;
  for (int i = 1, j = ((1 << (N + 1)) - 1); i <= (1 << N); i++, j--) {
    Q.push(j);
  }
  memset(b, 0, sizeof(b));
  while (!Q.empty()) {
    int f = Q.front();
    Q.pop();
    int s = Q.front();
    Q.pop();
    int n = f / 2;
    int big = Max(b[f] + a[f], b[s] + a[s]),
        small = Min(b[f] + a[f], b[s] + a[s]);
    ans = ans + big - small;
    b[n] = big;
    Q.push(n);
    if (n == 1) {
      break;
    }
  }
  cout << ans << endl;
}
int main() {
  while (scanf("%d", &N) != EOF) {
    work();
  }
  return 0;
}
