#include <bits/stdc++.h>
using namespace std;
const int INF = 1000001000;
const long long INFL = 2000000000000001000;
int solve();
int main() {
  srand(2317);
  cout.precision(10);
  cout.setf(ios::fixed);
  int tn = 1;
  for (int i = 0; i < tn; ++i) solve();
}
int a[1 << 15];
int base;
long long ans = 0;
int calc(int u) {
  if (u >= base) return 0;
  int l = calc(u * 2) + a[u * 2];
  int r = calc(u * 2 + 1) + a[u * 2 + 1];
  ans += abs(l - r);
  return max(l, r);
}
int solve() {
  int n;
  cin >> n;
  base = (1 << n);
  for (int i = 0; i < int((1 << (n + 1)) - 2); ++i) cin >> a[i + 2];
  calc(1);
  cout << ans << '\n';
  return 0;
}
