#include <bits/stdc++.h>
using namespace std;
int node[2048];
int expo(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1) res = res * a;
    b >>= 1;
    a *= a;
  }
  return res;
}
int M;
pair<int, int> adjust(int n) {
  if (2 * n > M) return make_pair(node[n], 0);
  pair<int, int> a1 = adjust((2 * (n)));
  pair<int, int> a2 = adjust((2 * (n) + 1));
  int d = abs(a1.first - a2.first);
  return make_pair(max(a1.first, a2.first) + node[n],
                   a1.second + a2.second + d);
}
int main() {
  ios::sync_with_stdio(0);
  int n, a;
  cin >> n;
  int nd = 2;
  while (cin >> a) node[nd++] = a;
  M = nd - 1;
  nd = expo(2, n);
  cout << adjust(1).second << "\n";
  return 0;
}
