#include <bits/stdc++.h>
using namespace std;
long long mp(long long i, long long n, vector<long long> &l,
             vector<long long> &mpdp) {
  if (mpdp[i] != -1) return mpdp[i];
  if (i * 2 >= n) {
    mpdp[i] = 0;
    return 0;
  }
  mpdp[i] = max(l[2 * i] + mp(2 * i, n, l, mpdp),
                l[2 * i + 1] + mp(2 * i + 1, n, l, mpdp));
  return mpdp[i];
}
long long util(long long i, long long n, vector<long long> &mpdp, long long lop,
               vector<long long> &l) {
  if (i * 2 + 1 < n) {
    long long a = 0;
    a = ((l[i * 2] + mpdp[i * 2] == lop)
             ? (lop - l[i * 2 + 1] - mpdp[i * 2 + 1])
             : (lop - l[i * 2] - mpdp[i * 2]));
    return a + util(i * 2 + 1, n, mpdp, mpdp[i * 2 + 1], l) +
           util(i * 2, n, mpdp, mpdp[i * 2], l);
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, lop = 0, a = 0;
  cin >> n;
  vector<long long> light(pow(2, n + 1)), mpdp(pow(2, n + 1), -1);
  for (long long i = 2; i < pow(2, n + 1); i++) cin >> light[i];
  lop = mp(1, pow(2, n + 1), light, mpdp);
  a = util(1, pow(2, n + 1), mpdp, lop, light);
  cout << a << endl;
  return 0;
}
