#include <bits/stdc++.h>
using namespace std;
long long n, a[100005], x = 1, maxm = 0, y = 0, x1 = 0, x2 = 0, ans = 0;
long long dfs(long long s, long long t) {
  if (s > 1) t += a[s - 1];
  if (s < x / 2) {
    dfs(s * 2, t);
    dfs(s * 2 + 1, t);
  } else {
    if (t > maxm) maxm = t;
  }
  return maxm;
}
long long dfs1(long long s, long long t) {
  if (s < x / 2) {
    x1 = dfs(s * 2, t);
    x2 = y - x1;
    a[s * 2 - 1] += x2;
    ans += x2;
    maxm = 0;
    x1 = dfs(s * 2 + 1, t);
    x2 = y - x1;
    a[s * 2] += x2;
    ans += x2;
    maxm = 0;
    if (s < x / 2) {
      dfs1(s * 2, t + a[s * 2 - 1]);
      dfs1(s * 2 + 1, t + a[s * 2]);
    }
  }
  return ans;
}
int main() {
  long long i;
  cin >> n;
  for (i = 1; i <= n + 1; i++) {
    x *= 2;
  }
  for (i = 1; i <= x - 2; i++) cin >> a[i];
  y = dfs(1, 0);
  maxm = 0;
  dfs1(1, 0);
  cout << ans;
  return 0;
}
