#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
int n, ans;
int dp[maxn];
int a[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  int ins;
  cin >> ins;
  n = (1 << (ins + 1)) - 1;
  for (int i = 2; i <= n; i++) cin >> a[i];
  for (int i = n / 2 + 1; i <= n; i++) {
    int p = i;
    while (p != 1) {
      dp[i] += a[p];
      p = p / 2;
    }
  }
  int maxx = 0;
  for (int i = n / 2 + 1; i <= n; i++) maxx = max(maxx, dp[i]);
  for (int i = n / 2 + 1; i <= n; i++) dp[i] = maxx - dp[i];
  for (int i = n; i >= 1; i--) {
    int minn = min(dp[2 * i], dp[2 * i + 1]);
    dp[2 * i] -= minn;
    dp[2 * i + 1] -= minn;
    dp[i] += minn;
  }
  for (int i = 1; i <= n; i++) ans += dp[i];
  cout << ans;
  return 0;
}
