#include <bits/stdc++.h>
const int N = 1e5 + 100;
int n;
int a[N];
int s[N];
void init() {
  std::cin >> n;
  n = (1 << (n + 1)) - 1;
  for (int i = 2; i <= n; i++) {
    scanf("%d", &a[i]);
  }
}
void dfs(int u, int &answer) {
  if (u > n) {
    return;
  }
  answer += std::abs((s[u << 1] + a[u << 1]) - (s[u << 1 | 1] + a[u << 1 | 1]));
  dfs(u << 1, answer);
  dfs(u << 1 | 1, answer);
}
void work() {
  for (int i = n / 2; i >= 1; i--) {
    s[i] = std::max(s[i << 1] + a[i << 1], s[i << 1 | 1] + a[i << 1 | 1]);
  }
  int answer = 0;
  dfs(1, answer);
  std::cout << answer << std::endl;
}
int main() {
  init();
  work();
  return 0;
}
