#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int SIZE = 1e6 + 10;
int a[SIZE], lv[SIZE];
int main() {
  int(n);
  scanf("%d", &n);
  int m = 1 << (n + 1);
  for (int i = (2); i < (m); ++i) {
    lv[i] = lv[i >> 1] + 1;
    scanf("%d", &(a[i]));
  }
  int an = 0;
  for (int i = (1 << n) - 1; i >= 1; i--) {
    int ma = max(a[i * 2], a[i * 2 + 1]);
    int mi = min(a[i * 2], a[i * 2 + 1]);
    an += ma - mi;
    a[i] += ma;
  }
  printf("%d\n", an);
  return 0;
}
