#include <bits/stdc++.h>
using namespace std;
vector<int> arr;
int n;
long long solve(int i) {
  if (i < 0) {
    return 0;
  }
  int tmp = i + 1;
  int mx = max(arr[tmp * 2], arr[tmp * 2 - 1]);
  arr[i] += mx;
  return abs(arr[tmp * 2] - arr[tmp * 2 - 1]) + solve(i - 1);
}
int main() {
  cin >> n;
  arr.push_back(0);
  for (int i = 0; i < pow(2, n + 1) - 2; i++) {
    int x;
    cin >> x;
    arr.push_back(x);
  }
  cout << solve(pow(2, n + 1) - pow(2, n) - 2) << endl;
  ;
  return 0;
}
