#include <bits/stdc++.h>
using namespace std;
int main() {
  int k;
  cin >> k;
  uint64_t n = (1 << k), nl = 1;
  uint64_t x[n];
  for (uint64_t& t : x) t = 0;
  while (k--) {
    nl *= 2;
    uint64_t pn = n / nl;
    for (int i = 0; i < nl; ++i) {
      int t;
      cin >> t;
      for (int j = 0; j < pn; ++j) x[i * pn + j] += t;
    }
  }
  uint64_t maxx = *max_element(x, x + n);
  uint64_t count = 0;
  for (int l = 2; l <= nl; l *= 2) {
    uint64_t pn = n / l;
    for (uint64_t i = 0; i < l; ++i) {
      uint64_t curmax = *max_element(x + i * pn, x + (i + 1) * pn);
      if (uint64_t diff = maxx - curmax) {
        for (uint64_t* t = x + i * pn; t < x + (i + 1) * pn; ++t) *t += diff;
        count += diff;
      }
    }
  }
  cout << count << endl;
  return 0;
}
