#include <bits/stdc++.h>
using namespace std;
int n, a[1 << 11], ans;
int main() {
  scanf("%d", &n);
  for (int i = 2; i < (1 << (n + 1)); i++) scanf("%d", &a[i]);
  for (int k = n; k; k--)
    for (int i = (1 << k); i < (1 << (k + 1)); i += 2) {
      a[i >> 1] += max(a[i], a[i + 1]);
      ans += max(a[i], a[i + 1]) - min(a[i], a[i + 1]);
    }
  printf("%d", ans);
  return 0;
}
