#include <bits/stdc++.h>
using namespace std;
template <class _T>
inline string tostr(const _T& a) {
  ostringstream os("");
  os << a;
  return os.str();
}
template <class T>
inline T sqr(const T& x) {
  return x * x;
};
template <class T1, class T2>
ostream& operator<<(ostream& c, pair<T1, T2> p) {
  return c << "(" << p.first << ", " << p.second << ")";
}
const int inf = 0x3f3f3f3f;
const double eps = 1e-7;
int n;
int v[1 << 13];
int lim;
int tot;
int ans;
int sum(int no) {
  if (no > lim) return 0;
  return v[no] + max(sum(2 * no + 1), sum(2 * no + 2));
}
void summ(int no, int sum) {
  if (no > lim) {
    tot = max(tot, sum);
    return;
  }
  summ(2 * no + 1, sum + v[no]);
  summ(2 * no + 2, sum + v[no]);
}
int bt(int no) {
  if (no > lim) {
    return 0;
  }
  int a = bt(2 * no + 1);
  int b = bt(2 * no + 2);
  ans += abs(a - b);
  return v[no] + max(a, b);
}
int main() {
  scanf("%d", &n);
  lim = (1 << (n + 1)) - 2;
  for (int i = 1, __ = lim; i <= __; i++) {
    scanf("%d", &v[i]);
  }
  summ(0, 0);
  bt(0);
  cout << ans << endl;
  return 0;
}
