#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, a, c, b, t = 0;
  vector<int> v;
  scanf("%d", &n);
  v.push_back(0);
  v.push_back(0);
  int n2 = (1 << (n + 1)) - 2;
  for (int i = 0; i < n2; i++) {
    scanf("%d", &c);
    v.push_back(c);
  }
  int depth = (1 << n) >> 1;
  vector<int> v2(1 << (n + 1));
  for (int i = depth; i > 0; i >>= 1) {
    int i2 = i << 1;
    for (int j = i; j < i2; j++) {
      a = j * 2;
      b = a + 1;
      a = v2[a] + v[a];
      b = v2[b] + v[b];
      t += fabs(a - b);
      v2[j] = max(a, b);
    }
  }
  printf("%d", t);
  return 0;
}
