#include <bits/stdc++.h>
using namespace std;
bool comp(int a, int b) { return (a > b); }
int n, data[5000];
pair<int, int> solved(int x, int level) {
  if (level == n)
    return make_pair(data[x], 0);
  else {
    pair<int, int> a = solved(x * 2, level + 1),
                   b = solved(x * 2 + 1, level + 1);
    return make_pair(data[x] + max(a.first, b.first),
                     a.second + b.second + abs(a.first - b.first));
  }
}
int main() {
  int maks = 1;
  pair<int, int> ans;
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) maks *= 2;
  for (int i = 2; i <= maks - 1; i++) scanf("%d", &data[i]);
  ans = solved(1, 0);
  printf("%d", ans.second);
  return 0;
}
