#include <bits/stdc++.h>
using namespace std;
int n, m = 0, k, ans = 0, a[1000000], b[1000000], x, y, z = 1, MAX, MIN;
string str, str1, str2;
char C;
bool flag = false;
int main() {
  cin >> n;
  k = pow(2, float(n - 1));
  n = pow(2, (float)(n + 1)) - 1;
  for (int i = 2; i <= n; i++) {
    cin >> a[i];
  }
  n--;
  for (int i = n; i >= 2; i -= 2) {
    a[i / 2] += max(a[i], a[i + 1]), ans += (abs(a[i] - a[i + 1]));
  }
  cout << ans << endl;
  return 0;
}
