#include <bits/stdc++.h>
using namespace std;
int pre[100010], sol[100010];
int main() {
  int n;
  scanf("%d", &n);
  int cnt = (1 << (n + 1)) - 1;
  for (int i = 2; i <= cnt; i++) {
    scanf("%d", &pre[i]);
  }
  int ans = 0;
  for (int i = cnt; i >= 1; i--) {
    if (i * 2 + 1 <= cnt) {
      int v1 = pre[i * 2] + sol[i * 2], v2 = pre[i * 2 + 1] + sol[i * 2 + 1];
      ans += max(v1, v2) - min(v1, v2);
      sol[i] = max(v1, v2);
    }
  }
  printf("%d\n", ans);
  return 0;
}
