#include <bits/stdc++.h>
using namespace std;
int n, a[3000], b[3000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  int m = (1 << (n + 1)) - 1;
  for (int i = 2; i <= m; ++i) cin >> a[i];
  long long ans = 0;
  for (int i = m; i >= 2; i -= 2) {
    int l = i - 1;
    int r = i;
    int v = i / 2;
    b[v] = max(a[l] + b[l], a[r] + b[r]);
    ans += (b[v] - (a[l] + b[l])) + (b[v] - (a[r] + b[r]));
  }
  cout << ans;
  return 0;
}
