#include <bits/stdc++.h>
using namespace std;
long long n, a[2050], dp[12], sum, maks, ans, tmp;
void dfs(long long x) {
  if (x >= dp[n]) {
    return;
  }
  a[2 * x] += a[x];
  a[2 * x + 1] += a[x];
  maks = max(maks, max(a[2 * x], a[2 * x + 1]));
  dfs(2 * x);
  dfs(2 * x + 1);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  dp[0] = 1;
  for (int i = 1; i <= n + 1; i++) {
    dp[i] = dp[i - 1] * 2;
  }
  for (int i = 2; i <= dp[n + 1] - 1; i++) {
    cin >> a[i];
  }
  dfs(1);
  for (int i = n - 1; i >= 0; i--) {
    for (int j = dp[n]; j < dp[n + 1]; j++) {
      tmp = max(tmp, a[j]);
      sum++;
      if (sum == dp[i]) {
        ans += maks - tmp;
        for (int k = j; k >= j - dp[i] + 1; k--) {
          a[k] += maks - tmp;
        }
        tmp = 0;
        sum = 0;
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
