#include <bits/stdc++.h>
using namespace std;
const int maxn = 1 << 12;
int len[maxn];
int maxNode;
int height[maxn];
int add[maxn];
int main() {
  int n;
  cin >> n;
  maxNode = (1 << (n + 1)) - 1;
  for (int i = 2; i <= (1 << (n + 1)) - 1; i++) {
    cin >> len[i];
  }
  int maxId = 1;
  height[1] = 0;
  for (int i = 1; i <= maxNode; i++) {
    if (height[i] > height[maxId]) {
      maxId = i;
    }
    height[i * 2] = height[i] + len[i * 2];
    height[i * 2 + 1] = height[i] + len[i * 2 + 1];
  }
  int maxLen = height[maxId];
  memset(add, 0, sizeof add);
  for (int i = (1 << n); i <= maxNode; i++) {
    add[i] = maxLen - height[i];
  }
  int ans = 0;
  for (int i = n; i >= 0; i--) {
    for (int j = (1 << i); j <= (1 << (i + 1)) - 1; j += 2) {
      int tmp = min(add[j], add[j ^ 1]);
      ans += (add[j] - tmp) + (add[j ^ 1] - tmp);
      add[j / 2] = tmp;
    }
  }
  cout << ans << endl;
  return 0;
}
