#include <bits/stdc++.h>
using namespace std;
int n;
int a[10000];
int main() {
  scanf("%d", &n);
  for (int i = (2), _for = (1 << (n + 1)); i < _for; ++i) scanf("%d", a + i);
  int R = 0;
  for (int i = (1 << n) - 1; i >= 1; --i) {
    int a = ::a[i * 2], b = ::a[i * 2 + 1];
    R += abs(a - b);
    ::a[i] += max(a, b);
  }
  printf("%d\n", R);
  return 0;
}
