#include <bits/stdc++.h>
using namespace std;
int n;
int vis[1 << 13];
long long arr[1 << 13];
int main() {
  memset(vis, 0, sizeof(vis));
  memset(arr, 0LL, sizeof(arr));
  cin >> n;
  int sz = (1 << (n + 1)) - 1;
  for (int i = 2; i <= sz; i++) cin >> arr[i];
  long long cnt = 0;
  for (int i = sz; i > 1; i -= 2) {
    arr[i / 2] += max(arr[i], arr[i - 1]);
    cnt += abs(arr[i] - arr[i - 1]);
  }
  cout << cnt;
  return 0;
}
