#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int m = (1 << (n + 1));
  int a[100005];
  int arr[100005];
  for (int i = 2; i < m; i++) {
    cin >> a[i];
  }
  int mx = 0;
  for (int i = (1 << n); i < m; i++) {
    int x = i;
    int cnt = 0;
    while (x != 1) {
      cnt += a[x];
      x >>= 1;
    }
    arr[i] = cnt;
    mx = max(mx, arr[i]);
  }
  int ans = 0;
  for (int i = (1 << n); i < m; i++) {
    arr[i] = (mx - arr[i]);
  }
  for (int level = n; level >= 1; level--) {
    int lim = (1 << (level + 1));
    for (int i = (1 << level); i < lim; i += 2) {
      ans += abs(arr[i] - arr[i + 1]);
      arr[i / 2] = min(arr[i], arr[i + 1]);
    }
  }
  cout << ans << endl;
  return 0;
}
