#include <bits/stdc++.h>
using namespace std;
vector<int> G[(1 << 12) + 5], C[(1 << 12) + 5];
long long DP[(1 << 12) + 5];
void dfs(int u) {
  for (int i = 0; i < ((int)(G[u]).size()); ++i) {
    int v = G[u][i];
    DP[v] = C[u][i] + DP[u];
    dfs(v);
  }
}
vector<vector<long long> > rmq;
long long query(int a, int b) {
  int r = 31 - __builtin_clz(b - a + 1);
  return min(rmq[r][a], rmq[r][b - (1 << r) + 1]);
}
vector<long long> v;
long long f(int lo, int hi, long long cur) {
  if (lo == hi) return v[lo] - cur;
  long long cost = query(lo, hi) - cur;
  int mid = (lo + hi) / 2;
  return cost + f(lo, mid, cur + cost) + f(mid + 1, hi, cur + cost);
}
int main() {
  int n;
  while (scanf("%d", &n) == 1) {
    for (int i = 2; i <= (1 << (n + 1)) - 1; ++i) {
      int u = i - 1;
      int pu = (i / 2) - 1;
      int x;
      scanf("%d", &x);
      G[pu].push_back(u);
      C[pu].push_back(x);
    }
    dfs(0);
    long long maxi = -(1 << 29);
    for (int i = (1 << n); i <= (1 << (n + 1)) - 1; ++i) {
      int u = i - 1;
      maxi = max(maxi, DP[u]);
    }
    v.clear();
    for (int i = (1 << n); i <= (1 << (n + 1)) - 1; ++i) {
      int u = i - 1;
      v.push_back(maxi - DP[u]);
    }
    rmq = vector<vector<long long> >(20 + 1,
                                     vector<long long>(((int)(v).size())));
    for (int i = 0; i < ((int)(v).size()); ++i) rmq[0][i] = v[i];
    for (int k = 1; k <= 20; ++k)
      for (int i = 0; i + (1 << k) <= ((int)(v).size()); ++i)
        rmq[k][i] = min(rmq[k - 1][i], rmq[k - 1][i + (1 << (k - 1))]);
    cout << f(0, ((int)(v).size()) - 1, 0) << '\n';
  }
}
