#include <bits/stdc++.h>
using namespace std;
int dis[5000], lit[5000];
int main() {
  int n, maxi = 0;
  scanf("%d", &n);
  int num = pow(2, n);
  for (int i = 1; i <= 2 * num - 2; i++) scanf("%d", &lit[i]);
  for (int i = 1; i <= num; i++) {
    int t = num + i - 1;
    for (int j = 1; j <= n; j++) {
      dis[i] += lit[t - 1];
      t /= 2;
    }
    if (dis[i] > maxi) maxi = dis[i];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int b = pow(2, i);
    for (int j = 1; j * b <= num; j++) {
      ans += abs(dis[j * b] - dis[j * b - b / 2]);
      dis[j * b] = max(dis[j * b], dis[j * b - b / 2]);
    }
  }
  printf("%d", ans);
  return 0;
}
