#include <bits/stdc++.h>
using namespace std;
int n;
int light[1 << 22];
int solve(int cur) {
  if (cur >= (1 << (n + 1))) {
    return 0;
  }
  int ans = solve(cur * 2) + solve(cur * 2 + 1);
  int maxi = max(light[cur * 2], light[cur * 2 + 1]);
  int mini = min(light[cur * 2], light[cur * 2 + 1]);
  ans += maxi - mini;
  light[cur] += maxi;
  return ans;
}
int main(int argc, const char* argv[]) {
  scanf("%d", &n);
  for (int i = 2; i < (1 << (n + 1)); i++) {
    scanf("%d", &light[i]);
  }
  printf("%d\n", solve(1));
  return 0;
}
