#include <bits/stdc++.h>
using namespace std;
struct tree {
  int a;
  struct tree *l;
  struct tree *r;
};
struct tree *t, *root;
long long int m, a[100005];
long long int ans = 0;
int po(int n, int r) {
  int ans = 1;
  for (int i = 1; i <= r; i++) {
    ans = n * ans;
  }
  return ans;
}
int dfs(struct tree *n) {
  if (n->l == 0 && n->r == 0) return 0;
  a[n->l->a] += dfs(n->l);
  a[n->r->a] += dfs(n->r);
  ans += abs(a[n->l->a] - a[n->r->a]);
  return max(a[n->l->a], a[n->r->a]);
}
struct tree *insert(struct tree *n, int i) {
  n = (struct tree *)malloc(sizeof(struct tree));
  if (i >= po(2, m)) {
    n->a = i;
    n->l = 0;
    n->r = 0;
  } else {
    n->a = i;
    if (i == 1) {
      root = n;
      root->l = insert(n->l, 2 * i);
      root->r = insert(n->r, 2 * i + 1);
    } else {
      n->l = insert(n->l, 2 * i);
      n->r = insert(n->r, 2 * i + 1);
    }
  }
  return n;
}
int main() {
  long long int j, i;
  cin >> m;
  i = po(2, m + 1) - 2;
  for (j = 2; j < i + 2; j++) {
    cin >> a[j];
  }
  insert(t, 1);
  dfs(root);
  cout << ans;
  return 0;
}
