#include <bits/stdc++.h>
using namespace std;
int h;
int edge[5000];
int extend[5000];
int maximum;
int v[5000];
bool flag = 0;
void dfs_m(int p, int value) {
  v[p] = value;
  if (edge[p * 2] != 0)
    dfs_m(p * 2 + 0, value + edge[p * 2]);
  else {
    if (maximum < value) maximum = value;
  }
  if (edge[p * 2 + 1] != 0)
    dfs_m(p * 2 + 1, value + edge[p * 2 + 1]);
  else {
    if (maximum < value) maximum = value;
  }
}
void dfs(int p, int value) {
  if (edge[p * 2] != 0) {
    dfs(p * 2, value + edge[p * 2] + extend[p * 2]);
  } else {
    if (value < maximum) {
      extend[p] += (maximum - value);
    }
  }
  if (edge[p * 2 + 1] != 0) {
    dfs(p * 2 + 1, value + edge[p * 2 + 1] + extend[p * 2 + 1]);
  } else {
    if (value < maximum) {
      extend[p] += (maximum - value);
    }
  }
}
int min(int a, int b) {
  if (a == b) return -1;
  if (a < b) {
    return a;
  }
  return b;
}
void merge(int n) {
  if (extend[n * 2] != 0 && extend[n * 2 + 1] != 0) {
    int m = min(extend[n * 2 + 1], extend[n * 2]);
    flag = false;
    if (m == -1) {
      extend[n] += extend[n * 2 + 1];
      extend[n * 2] = extend[n * 2 + 1] = 0;
      merge(n / 2);
    } else {
      extend[n] += m;
      extend[n * 2] -= m;
      extend[n * 2 + 1] -= m;
      merge(n / 2);
    }
  }
}
int main() {
  scanf("%d", &h);
  int e = pow(2, h + 1) - 2;
  memset(edge, 0, sizeof(edge));
  memset(extend, 0, sizeof(extend));
  memset(v, 0, sizeof(v));
  for (int i = 2; i <= e + 1; i++) {
    scanf("%d", &edge[i]);
  }
  maximum = 0;
  dfs_m(1, 0);
  dfs(1, 0);
  int node = pow(2, h + 1);
  for (int i = 2; i <= e + 1; i++) {
    extend[i] /= 2;
  }
  while (true) {
    flag = true;
    for (int i = 0; i < node; i++) {
      merge(i);
    }
    for (int i = 2; i <= e + 1; i++) {
    }
    if (flag) {
      break;
    }
  }
  int ans = 0;
  for (int i = 0; i <= e + 1; i++) {
    ans += extend[i];
  }
  printf("%d", ans);
  return 0;
}
