#include <bits/stdc++.h>
using namespace std;
int head[100001];
int edge;
bool v[100001];
int fx[100001];
int ans = 0;
struct line {
  int s, t;
  int x;
  int next;
} a[100001];
inline void add(int s, int t, int x) {
  a[edge].next = head[s];
  head[s] = edge;
  a[edge].s = s;
  a[edge].t = t;
  a[edge].x = x;
}
inline int absx(int x) {
  if (x < 0) x = -x;
  return x;
}
inline void dfs(int d) {
  v[d] = true;
  int i;
  for (i = head[d]; i != 0; i = a[i].next) {
    int t = a[i].t;
    if (!v[t]) {
      dfs(t);
    }
  }
  int s = 0, s1 = 0, s2 = 0;
  for (i = head[d]; i != 0; i = a[i].next) {
    int t = a[i].t;
    if (t != d / 2) {
      if (s == 0) {
        s++;
        s1 = a[i].x;
      } else
        s2 = a[i].x;
    }
  }
  ans += absx(s2 + fx[d * 2] - (s1 + fx[d * 2 + 1]));
  fx[d] += max(s2 + fx[d * 2], s1 + fx[d * 2 + 1]);
}
int main() {
  int n;
  scanf("%d", &n);
  int i, j;
  int s = 0;
  int d = 1;
  int x;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= 1 << i; j++) {
      scanf("%d", &x);
      if (s == 0) {
        s++;
        edge++;
        add(d, d * 2, x);
        edge++;
        add(d * 2, d, x);
      } else {
        s = 0;
        edge++;
        add(d, d * 2 + 1, x);
        edge++;
        add(d * 2 + 1, d, x);
        d++;
      }
    }
  }
  dfs(1);
  printf("%d\n", ans);
  return 0;
}
