#include <bits/stdc++.h>
using namespace std;
int nodes[2050], depth, score = 0;
int abs(int a) {
  if (a > 0) return a;
  return -a;
}
int max(int a, int b) {
  if (a > b) return a;
  return b;
}
int dfs(int d, int i, int total) {
  if (d == depth) return nodes[i];
  int r = dfs(d + 1, i * 2, 0);
  int l = dfs(d + 1, i * 2 + 1, 0);
  score += abs(r - l);
  return max(r, l) + nodes[i];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> depth;
  for (size_t i = 2; i < pow(2, depth + 1); i++) cin >> nodes[i];
  dfs(0, 1, 0);
  cout << score;
}
