#include <bits/stdc++.h>
using namespace std;
int arr[100010];
int ans1[100010];
int j = 0;
int n;
int maxi = -1;
int val = 0;
void solve(int index, int level, int ans) {
  if (level == n) {
    ans = ans + arr[index];
    ans1[j++] = ans;
  } else {
    ans = ans + arr[index];
    level++;
    solve(2 * index + 2, level, ans);
    solve(2 * index + 3, level, ans);
  }
}
void answer(int index1, int index2, int level) {
  int i;
  int m = -1, c;
  if (level == n) {
    val = val + maxi - ans1[index1];
  } else {
    for (i = index1; i <= index2; i++) {
      m = max(m, ans1[i]);
    }
    m = maxi - m;
    val = val + m;
    for (i = index1; i <= index2; i++) ans1[i] += m;
    int mid = index1 + (index2 - index1) / 2;
    level++;
    answer(index1, mid, level);
    answer(mid + 1, index2, level);
  }
}
int main() {
  cin >> n;
  int i, l, r;
  for (i = 0; i < pow(2, n + 1) - 2; i++) {
    cin >> arr[i];
  }
  solve(0, 1, 0);
  solve(1, 1, 0);
  for (i = 0; i < j; i++) {
    maxi = max(maxi, ans1[i]);
  }
  l = 0;
  r = j - 1;
  int mid = l + (r - l) / 2;
  answer(l, mid, 1);
  answer(mid + 1, r, 1);
  cout << val << endl;
}
