#include <bits/stdc++.h>
using namespace std;
int A[1 << 11];
int main() {
  int n, path_sum = 0, max_pathsum = -1, final_ans = 0;
  scanf("%d", &n);
  int val = 1 << (n + 1);
  val--;
  for (int i = 1; i <= val - 1; i++) scanf("%d", &A[i]);
  for (int i = (1 << n); i <= val; i++) {
    int start = i;
    path_sum = 0;
    for (int j = 1; j <= n; j++) {
      path_sum += A[start - 1];
      start = start / 2;
    }
    max_pathsum = max(max_pathsum, path_sum);
  }
  for (int i = (1 << (n - 1)); i >= 2; i = i / 2) {
    for (int j = 1; j <= (1 << n) / (i); j++) {
      int starting = (j - 1) * i + (1 << n);
      int mini = INT_MAX;
      for (int k = starting; k < starting + i; k++) {
        path_sum = 0;
        int node = k;
        for (int bb = 1; bb <= n; bb++) {
          path_sum += A[node - 1];
          node = node / 2;
        }
        mini = min((max_pathsum - path_sum), mini);
      }
      for (int k = starting; k < starting + i; k++) A[k - 1] += mini;
      final_ans += mini;
    }
  }
  for (int i = (1 << n); i <= val; i++) {
    int start = i;
    path_sum = 0;
    for (int j = 1; j <= n; j++) {
      path_sum += A[start - 1];
      start = start / 2;
    }
    final_ans += max_pathsum - path_sum;
  }
  printf("%d\n", final_ans);
  return 0;
}
