#include <bits/stdc++.h>
using namespace std;
int lights[1 << ((10) + 1)];
int sums[1 << ((10) + 1)];
int n;
int find_added(int root) {
  int added;
  if (root >= (1 << n)) {
    sums[root] = 0;
    return 0;
  }
  int left_added = find_added(root * 2);
  int right_added = find_added(root * 2 + 1);
  int diff = abs(sums[root * 2] + lights[root * 2] -
                 (sums[root * 2 + 1] + lights[root * 2 + 1]));
  sums[root] = max(sums[root * 2] + lights[root * 2],
                   sums[root * 2 + 1] + lights[root * 2 + 1]);
  return left_added + right_added + diff;
}
int main() {
  cin >> n;
  for (int i = 2; i < (1 << (n + 1)); i++) {
    cin >> lights[i];
  }
  int added = find_added(1);
  cout << added << "\n";
  return 0;
}
