#include <bits/stdc++.h>
using namespace std;
int abso(int a, int b) {
  if (a < b)
    return b - a;
  else
    return a - b;
}
long long ans = 0, temp;
void recurse(int node, int arr[]) {
  int x1 = 2 * node;
  int x2 = 2 * node + 1;
  if (x1 > temp) return;
  recurse(2 * node, arr);
  recurse(2 * node + 1, arr);
  ans += (max(arr[2 * node], arr[2 * node + 1]) -
          min(arr[2 * node], arr[2 * node + 1]));
  arr[node] += max(arr[2 * node], arr[2 * node + 1]);
}
int main() {
  int n;
  cin >> n;
  temp = (1 << (n + 1)) - 1;
  int arr[100000];
  for (int i = 2; i <= temp; i++) cin >> arr[i];
  recurse(1, arr);
  cout << ans << endl;
  return 0;
}
