#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long llinf = (long long)inf * inf;
const long double eps = 1e-6;
const int maxn = 11000;
const long double pi = acos(-1.0);
template <typename T>
T sqr(T x) {
  return x * x;
}
int n, ans;
int a[11000], b[11000];
void dfs(int x, int d) {
  if (d == n) {
    return;
  }
  dfs(x * 2, d + 1);
  dfs(x * 2 + 1, d + 1);
  b[x] = max(b[x * 2] + a[x * 2], b[x * 2 + 1] + a[x * 2 + 1]);
  ans += b[x] - b[x * 2] - a[x * 2];
  ans += b[x] - b[x * 2 + 1] - a[x * 2 + 1];
}
int main() {
  scanf("%d", &n);
  int m = 1 << (n + 1);
  for (int i = 2; i < m; i++) {
    scanf("%d", &a[i]);
  }
  dfs(1, 0);
  cout << ans << endl;
  return 0;
}
