#include <bits/stdc++.h>
using namespace std;
const int inf = int(1e9);
const double eps = 1e-9;
const double pi = 4 * atan(double(1));
const int N = (1 << 15);
long long ans;
int maxv;
int a[N];
void dfs(int v, int need, long long add) {
  if (v >= maxv) {
    return;
  }
  long long cur = need - (a[v * 2] + add);
  assert(cur >= 0);
  ans += cur;
  dfs(v * 2, need, add + cur);
  cur = need - (a[v * 2 + 1] + add);
  ans += cur;
  dfs(v * 2 + 1, need, add + cur);
}
int main() {
  int n;
  cin >> n;
  maxv = 1;
  for (int i = 0; i < n; ++i) {
    maxv *= 2;
  }
  for (int i = 2; i < 2 * maxv; ++i) {
    scanf("%d", a + i);
    a[i] += a[i / 2];
  }
  for (int i = maxv - 1; i > 0; --i) {
    a[i] = max(a[i * 2], a[i * 2 + 1]);
  }
  ans = 0;
  dfs(1, a[1], 0);
  cout << ans << endl;
  return 0;
}
