#include <bits/stdc++.h>
using namespace std;
inline int read() {
  static int r, sign;
  static char c;
  r = 0, sign = 1;
  do c = getchar();
  while (c != '-' && (c < '0' || c > '9'));
  if (c == '-') sign = -1, c = getchar();
  while (c >= '0' && c <= '9') r = r * 10 + (int)(c - '0'), c = getchar();
  return sign * r;
}
template <typename T>
inline void print(T *a, int n) {
  for (int i = 1; i < n; ++i) cout << a[i] << " ";
  cout << a[n] << endl;
}
int n, m, a[10000 + 1], f[10000 + 1];
long long ans = 0LL;
void dfs(int x) {
  if (x > n) return;
  int ls = x * 2, rs = x * 2 + 1;
  dfs(ls);
  dfs(rs);
  f[x] = max(f[ls] + a[ls], f[rs] + a[rs]);
  ans += f[x] - f[ls] - a[ls];
  ans += f[x] - f[rs] - a[rs];
}
int main(int argc, char *argv[]) {
  scanf("%d", &m);
  n = (1 << (m + 1)) - 1;
  for (int i = 2; i <= n; ++i) scanf("%d", a + i);
  dfs(1);
  cout << ans << endl;
  fclose(stdin);
  fclose(stdout);
  return 0;
}
