#include <bits/stdc++.h>
using namespace std;
const int nm = 2100;
int n, m, a[nm], mm, cr, kq;
void tinh(int i) {
  cr += a[i];
  if (cr > mm) mm = cr;
  if (i < (1 << n)) {
    tinh(i << 1);
    tinh((i << 1) + 1);
  }
  cr -= a[i];
}
int tinh2(int i, int left) {
  left -= a[i];
  if (i < (1 << n)) {
    int x = tinh2(i << 1, left);
    int y = tinh2((i << 1) + 1, left);
    kq += max(x, y) - min(x, y);
    return left - a[i << 1] - x + min(x, y);
  }
  return left;
}
int dfs(int i, int cr) {
  if (i >= (1 << n)) return cr;
  int x = dfs(i << 1, cr + a[i << 1]);
  int y = dfs((i << 1) + 1, cr + a[(i << 1) + 1]);
  kq += max(x, y) - min(x, y);
  return max(x, y);
}
int main() {
  scanf("%d", &n);
  m = (1 << (n + 1)) - 1;
  for (int i = 2; i <= m; ++i) {
    scanf("%d", &a[i]);
  }
  dfs(1, 0);
  printf("%d\n", kq);
}
