#include <bits/stdc++.h>
using namespace std;
int n, ans = 0;
vector<int> result, lights;
void round_graph(vector<int> a, int v, int sum) {
  if (v >= 1 << n) {
    result[v - (1 << n)] = a[v] + sum;
  } else {
    round_graph(a, v * 2, a[v] + sum);
    round_graph(a, v * 2 + 1, a[v] + sum);
  }
}
int round_result(vector<int> a, int l, int r) {
  if (l == r) {
    return a[l];
  }
  int m = (l + r) / 2;
  int left = round_result(a, l, m);
  int right = round_result(a, m + 1, r);
  ans += abs(left - right);
  return max(left, right);
}
int main() {
  cin >> n;
  result.resize(((unsigned int)(1 << n)), 0);
  lights.resize((unsigned int)(1 << (n + 1)), 0);
  for (int i = 2; i < lights.size(); i++) {
    cin >> lights[i];
  }
  round_graph(lights, 1, 0);
  round_result(result, 0, result.size() - 1);
  cout << ans << "\n";
  return 0;
}
