#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<int> v(2050);
  int N;
  cin >> N;
  long long ans = 0;
  int siz = (int)pow(2.0, N + 1);
  v[0] = 0;
  v[1] = 0;
  for (int i = 2; i < siz; ++i) cin >> v[i];
  for (int i = siz - 1; i > 1; i -= 2) {
    v[i / 2] += max(v[i], v[i - 1]);
    ans += abs(v[i] - v[i - 1]);
  }
  cout << ans;
  return 0;
}
