#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
int num[1 << 15], dp[1 << 15];
int n;
long long ans = 0;
void dfs(int now) {
  if (2 * now >= (1 << (n + 1))) {
    dp[now] = 0;
    return;
  }
  dfs(2 * now);
  dfs(2 * now + 1);
  dp[now] = max(dp[2 * now] + num[2 * (now - 1)],
                dp[2 * now + 1] + num[2 * (now - 1) + 1]);
  ans += abs(dp[2 * now] + num[2 * (now - 1)] - dp[2 * now + 1] -
             num[2 * (now - 1) + 1]);
}
int main() {
  while (cin >> n) {
    for (int i = 0; i < (1 << (n + 1)) - 2; i++) {
      int x;
      scanf("%d", num + i);
    }
    ans = 0;
    dfs(1);
    cout << ans << endl;
  }
  return 0;
}
