#include <bits/stdc++.h>
using namespace std;
int n;
int k = 1;
int a[10000];
int counter[10000];
int maximum;
int cur_sum = 0;
void dfs_1(int vert) {
  cur_sum += a[vert];
  if (vert * 2 + 1 < k) {
    dfs_1(2 * vert);
    dfs_1(2 * vert + 1);
  } else {
    counter[vert - k / 2 + 1] = cur_sum;
  }
  cur_sum -= a[vert];
}
void dfs_2(int left, int right) {
  int cur_max = 0;
  int middle = (right + left) / 2;
  for (int i = left; i <= right; i++) {
    if (counter[i] > cur_max) cur_max = counter[i];
  }
  for (int i = left; i <= right; i++) {
    counter[i] += (maximum - cur_max);
  }
  cur_sum += (maximum - cur_max);
  if (left < right) {
    dfs_2(middle + 1, right);
    dfs_2(left, middle);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i <= n; i++) {
    k *= 2;
  }
  for (int i = 2; i <= k; i++) {
    cin >> a[i];
  }
  dfs_1(1);
  for (int i = 1; i <= k / 2; i++) {
    if (counter[i] > maximum) maximum = counter[i];
  }
  cur_sum = 0;
  dfs_2(1, k / 2);
  cout << cur_sum;
  return 0;
}
