#include <bits/stdc++.h>
using namespace std;
bool comp(int a, int b) { return (a > b); }
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int c[10000];
int pow2(int n) {
  int p = 1;
  for (int i = 0; i < n; ++i) p *= 2;
  return p;
}
int main(void) {
  int n;
  cin >> n;
  int a[pow2(n + 1)], i, ans = 0;
  for (i = 0; i < pow2(n + 1) - 2; ++i) cin >> a[i];
  for (i = pow2(n + 1) - 1; i > 1; --i) {
    c[i] = max(c[i * 2], c[i * 2 + 1]) + a[i - 2];
    ans += max(c[i * 2], c[i * 2 + 1]) - min(c[i * 2], c[i * 2 + 1]);
    c[i * 2] = c[i * 2 + 1] = max(c[i * 2], c[i * 2 + 1]);
  }
  ans += max(c[2], c[3]) - min(c[2], c[3]);
  cout << ans;
  return 0;
}
