#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int count = 0;
  int a[(int)(pow(2.0, (double)(n + 1)) - 2)];
  int x = (int)pow(2.0, (double)(n + 1)) - 2;
  for (int i = 0; i < x; i++) {
    cin >> a[i];
  }
  int y = (int)(pow(2.0, (double)(n + 1)) - pow(2.0, (double)n));
  int b[y];
  for (int i = 0; i < y; i++) b[i] = 0;
  for (int i = 0; i < y; i++) {
    for (int j = 0; j < n; j = j + 1)
      b[i] = b[i] +
             a[(int)((pow(2.0, (double)n) + i) / (pow(2.0, (double)j))) - 2];
  }
  for (int j = 1; j <= n; j++)
    for (int i = y - 1; i >= 0; i = i - (int)pow(2.0, (double)j)) {
      count = count + abs(b[i] - b[i - (int)pow(2.0, (double)(j - 1))]);
      if (b[i] > b[i - (int)pow(2.0, (double)(j - 1))])
        b[i - (int)pow(2.0, (double)(j - 1))] = b[i];
      else
        b[i] = b[i - (int)pow(2.0, (double)(j - 1))];
    }
  cout << count << endl;
}
