#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > adj[(1 << 12)];
int dist[(1 << 12) >> 1];
int intervals[(1 << 12) << 1], lazy[(1 << 12) << 1];
int lst;
void relax(int p, int val) {
  intervals[p] += val;
  lazy[p] += val;
}
void update(int s, int e, int p, int us, int ue, int val, int laz) {
  relax(p, laz);
  if (us <= s && e <= ue) {
    intervals[p] += val;
    lazy[p] += val;
    return;
  }
  if (s > ue || e < us) return;
  int mid = (s + e) >> 1;
  update(s, mid, p << 1, us, ue, val, lazy[p]);
  update(mid + 1, e, (p << 1) + 1, us, ue, val, lazy[p]);
  lazy[p] = 0;
  intervals[p] = max(intervals[p << 1], intervals[(p << 1) + 1]);
}
void build(int s, int e, int p) {
  if (s == e) {
    intervals[p] = dist[s - 1];
    return;
  }
  int mid = (s + e) >> 1;
  build(s, mid, p << 1);
  build(mid + 1, e, (p << 1) + 1);
  intervals[p] = max(intervals[p << 1], intervals[(p << 1) + 1]);
}
int query(int s, int e, int p, int qs, int qe, int laz) {
  relax(p, laz);
  if (qs <= s && e <= qe) return intervals[p];
  if (s > qe || e < qs) return 0;
  int mid = (s + e) >> 1;
  int ret = max(query(s, mid, p << 1, qs, qe, lazy[p]),
                query(mid + 1, e, (p << 1) + 1, qs, qe, lazy[p]));
  lazy[p] = 0;
  return ret;
}
int rec(int src, int sum) {
  int mx = sum;
  if (adj[src].empty()) dist[lst++] = sum;
  for (int i = 0; i < adj[src].size(); i++) {
    int t = adj[src][i].first;
    int cost = adj[src][i].second;
    mx = max(mx, rec(t, sum + cost));
  }
  return mx;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int mx_e = (1 << (n + 1));
  for (int i = 2; i < mx_e; i++) {
    int x;
    cin >> x;
    adj[i / 2].push_back(make_pair((i), (x)));
  }
  int must = rec(1, 0);
  int s = 1, e = (1 << n);
  build(s, e, 1);
  int ans = 0;
  for (int i = 1 << (n - 1); i; i >>= 1) {
    for (int beg = 1; beg <= (1 << n); beg += i) {
      int mx = 0;
      mx = query(s, e, 1, beg, beg + i - 1, 0);
      int inc = must - mx;
      ans += inc;
      update(s, e, 1, beg, beg + i - 1, inc, 0);
    }
  }
  cout << ans << "\n";
  return 0;
}
