#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int mod = 1000000007;
const int INF = 2147483647;
struct node {
  int r, l, p;
  int s;
  int sum;
  node() : r(-1), l(-1), p(-1), sum(-1) {}
};
node d[2048];
int dfs(int n, int m) {
  if (d[n].r == -1) {
    d[n].sum = d[n].s + m;
    return d[n].s + m;
  }
  return max(dfs(d[n].l, d[n].s + m), dfs(d[n].r, d[n].s + m));
}
int Max;
int Sum = 0;
int up(int r, int l, int c) {
  int val = INF;
  for (int i = r; i < l; i++) {
    val = min(Max - d[i].sum, val);
  }
  for (int i = r; i < l; i++) {
    d[i].sum += val;
  }
  Sum += val;
  if (r != l - 1) {
    return up(r, l - (l - r) / 2, c * 2) + up(l - (l - r) / 2, l, c * 2 + 1) +
           val;
  } else
    return val;
}
int sup(int r, int l) {
  return up(r, l - (l - r) / 2, 2) + up(l - (l - r) / 2, l, 3);
}
int main() {
  int n;
  cin >> n;
  int m = pow(2, n + 1);
  d[1].p = -1;
  for (int i = 2; i < m; i++) {
    d[i].p = i / 2;
    if (i % 2)
      d[i / 2].r = i;
    else
      d[i / 2].l = i;
  }
  d[1].s = 0;
  for (int i = 2; i < m; i++) {
    cin >> d[i].s;
  }
  Max = dfs(1, 0);
  cout << sup(pow(2, n), m) << endl;
}
