#include <bits/stdc++.h>
using namespace std;
int main() {
  void solution();
  solution();
  return 0;
}
const int maxn = 10;
const int maxm = 1 << (maxn + 1);
int a[maxm];
struct s {
  int append;
  int roads;
};
struct s count(int x, int m) {
  struct s res = {
      .append = 0,
      .roads = a[x],
  };
  if (x * 2 >= m) return res;
  struct s l = count(x * 2, m);
  struct s r = count(x * 2 + 1, m);
  res.roads += max(l.roads, r.roads);
  res.append += l.append + r.append;
  res.append += abs(l.roads - r.roads);
  return res;
}
void solution() {
  int n;
  scanf("%i", &n);
  int m = 1 << (n + 1);
  for (int i = 2; i < m; i++) {
    scanf("%i", &a[i]);
  }
  printf("%i\n", count(1, m).append);
}
