#include <bits/stdc++.h>
using namespace std;
int n, a[5005], m, ans, t[5005];
int main() {
  scanf("%d", &n);
  m = (1 << n + 1) - 1;
  for (int i = 2; i <= m; ++i) scanf("%d", a + i);
  for (int i = (1 << n) - 1; i >= 1; --i) {
    t[i] = max(t[i << 1] + a[i << 1], t[i << 1 | 1] + a[i << 1 | 1]);
    ans += t[i << 1] + a[i << 1] - t[i] + t[i << 1 | 1] + a[i << 1 | 1] - t[i];
  }
  printf("%d\n", ans * -1);
}
