#include <bits/stdc++.h>
using namespace std;
const int nax = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const int inf = (int)1e9;
const long long infll = (long long)1e18;
const long double PI = 3.1415926535897932384626433832795;
vector<int> a;
int n;
int maxi = 0;
void add_light(int index, int x) {
  if (index >= (1 << (n + 1))) {
    return;
  }
  a[index] += x;
  add_light(index << 1, x);
  add_light(index << 1 | 1, x);
}
void solve() {
  cin >> n;
  a.resize(1 << (n + 1));
  for (int i = 2; i < 1 << (n + 1); ++i) {
    cin >> a[i];
    a[i] += a[i >> 1];
    maxi = max(maxi, a[i]);
  }
  for (int i = (1 << n) - 1; i >= 2; --i) {
    a[i] = max(a[i << 1], a[i << 1 | 1]);
  }
  int ans = 0;
  for (int i = 2; i < 1 << (n + 1); ++i) {
    ans += (maxi - a[i]);
    add_light(i, maxi - a[i]);
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
