#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int N = (1 << (n + 1)) - 2;
  vector<int> v(N + 2), w(N + 2), q(N + 2);
  for (int i = 0; i < (N); ++i) cin >> v[i + 2];
  int t = 0, t2 = 0;
  for (int i = 0; i < (N + 1); ++i) t += v[i];
  for (int i = N + 1; i > 0; i -= 2) {
    int ttt = max(q[i - 1] + v[i - 1], q[i] + v[i]);
    w[i] = ttt - q[i] - v[i];
    w[i - 1] = ttt - q[i - 1] - v[i - 1];
    q[i / 2] = ttt;
  }
  for (int i(1); i < (N + 2); ++i) t2 += w[i];
  cout << t2 << endl;
  return 0;
}
