#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int N = (1 << (n + 1));
  vector<int> lights(N, 0);
  for (int i = 2; i < N; i++) cin >> lights[i];
  vector<int> path(N, 0);
  int ans = 0;
  for (int i = N / 2 - 1; i > 0; i--) {
    int left = path[2 * i] + lights[2 * i];
    int right = path[2 * i + 1] + lights[2 * i + 1];
    ans += abs(right - left);
    path[i] = max(right, left);
  }
  cout << ans << endl;
}
