#include <bits/stdc++.h>
using namespace std;
int n;
int cost[2048][2048];
int res;
int calc(int u) {
  if (u * 2 > ((1 << (n + 1)) - 1)) return 0;
  int a = cost[u][u * 2] + calc(u * 2);
  int b = cost[u][u * 2 + 1] + calc(u * 2 + 1);
  res += max(a, b) - min(a, b);
  return max(a, b);
}
int main() {
  scanf("%d", &n);
  for (int i = 2, z = 1, c; z <= (1 << (n + 1)) - 2; ++i, ++z) {
    scanf("%d", &c);
    cost[i / 2][i] = c;
  }
  calc(1);
  printf("%d\n", res);
  return 0;
}
