#include <bits/stdc++.h>
using namespace std;
int t[5000][2];
int main() {
  int n;
  scanf("%d", &n);
  int d = pow(2, n + 1);
  int dep = d - 1;
  memset(t, 0, sizeof(t));
  for (int i = 1; i <= dep / 2; i++) scanf("%d%d", &t[i][0], &t[i][1]);
  int ans = 0;
  for (int i = dep / 2; i >= 1; i--) {
    int s1 = 0, s2 = 0;
    for (int j = i; j <= dep; j = j * 2) s1 += t[j][0];
    for (int j = i; j <= dep; j = 2 * j + 1) s2 += t[j][1];
    if (s1 >= s2)
      t[i][1] += (s1 - s2);
    else
      t[i][0] += (s2 - s1);
    ans += abs(s1 - s2);
  }
  printf("%d\n", ans);
}
