#include <bits/stdc++.h>
using namespace std;
int Tree[5000], Dis[5000], MaxLen[5000];
int N, Num, MaxDis = -1, Ans = 0;
int DFSA(int u, int deep) {
  if (deep == N) {
    MaxLen[u] = 0;
    return 0;
  }
  Dis[u << 1] = Dis[u] + Tree[u << 1];
  Dis[u << 1 | 1] = Dis[u] + Tree[u << 1 | 1];
  MaxLen[u] = max(DFSA(u << 1, deep + 1) + Tree[u << 1],
                  DFSA(u << 1 | 1, deep + 1) + Tree[u << 1 | 1]);
  return MaxLen[u];
}
void DFSB(int u, int deep, int Delta) {
  int temp = MaxDis - Dis[u] - Delta - MaxLen[u];
  Ans += temp;
  if (deep == N) return;
  DFSB(u << 1, deep + 1, Delta + temp);
  DFSB(u << 1 | 1, deep + 1, Delta + temp);
  return;
}
int main() {
  cin >> N;
  Num = (1 << (N + 1)) - 1;
  for (register int i = 1; i <= Num - 1; ++i) cin >> Tree[i + 1];
  DFSA(1, 0);
  for (register int i = 1; i <= Num; ++i) MaxDis = max(MaxDis, Dis[i]);
  DFSB(1, 0, 0);
  cout << Ans << endl;
  return 0;
}
