#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
vector<int> vc[N + 10];
int a[N + 10], dp[N + 10], mxsub[N + 10];
int ans;
void DFS(int v, int pr = 0) {
  dp[v] = dp[pr] + a[v], mxsub[v] = dp[v];
  for (int i = 0; i < vc[v].size(); i++) {
    int u = vc[v][i];
    DFS(u, v);
    mxsub[v] = max(mxsub[v], mxsub[u]);
  }
  return;
}
void add_light(int v, int mx) {
  for (int i = 0; i < vc[v].size(); i++) {
    int u = vc[v][i];
    ans += mx - mxsub[u], add_light(u, mx - (mx - mxsub[u]));
  }
  return;
}
int main() {
  int n;
  cin >> n;
  for (int i = 2; i < (1 << (n + 1)); i++) cin >> a[i];
  for (int i = 1; i < (1 << n); i++)
    vc[i].push_back(2 * i), vc[i].push_back(2 * i + 1);
  DFS(1);
  int mx = 0;
  for (int i = (1 << n); i < (1 << (n + 1)); i++) mx = max(mx, dp[i]);
  add_light(1, mx);
  cout << ans;
  return 0;
}
