#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long long int n;
  cin >> n;
  long long int aux = 1 << (n + 1);
  long long int arr[aux];
  arr[0] = 0;
  arr[1] = 0;
  for (int i = 0; i < (aux - 2); ++i) cin >> arr[i + 2];
  long long ans = 0;
  long long spoint = 1 << (n + 1);
  while (1) {
    spoint = spoint >> 1;
    if (spoint == 1) break;
    for (int i = spoint; i < spoint << 1; i += 2) {
      ans += max(arr[i], arr[i + 1]) - min(arr[i], arr[i + 1]);
      arr[i / 2] += max(arr[i], arr[i + 1]);
    }
  }
  cout << ans;
  return 0;
}
