#include <bits/stdc++.h>
const int MAXN = 2 << 11;
long long ans;
int n;
int lit[MAXN];
int dp[MAXN];
int tot;
int dfs(int x) {
  dp[x] = lit[x];
  if ((x << 1) <= tot) dp[x] += std::max(dfs(x << 1), dfs((x << 1) + 1));
  return dp[x];
}
void dfs2(int x, long long num) {
  int change = num - dp[x];
  ans += change;
  num -= change + lit[x];
  if ((x << 1) <= tot) {
    dfs2((x << 1), num);
    dfs2((x << 1) + 1, num);
  }
}
int main(void) {
  scanf("%d", &n);
  tot = (1 << (n + 1)) - 1;
  for (int i = 2; i <= tot; i++) scanf("%d", &lit[i]);
  dfs(1);
  dfs2(1, dp[1]);
  std::cout << ans;
  return 0;
}
