#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > adj[1234567];
bool visited[1234567];
long long edge, nodes;
long long a[1234567];
long long weight[1234567];
struct item {
  long long mx, ans;
  item() {
    ans = 0;
    mx = 0;
  }
};
item tree[110];
void build(long long node) {
  if (2 * node > nodes) {
    return;
  }
  adj[node].push_back(make_pair(node * 2, a[2 * node - 1]));
  adj[node].push_back(make_pair(node * 2 + 1, a[2 * node]));
  build(node * 2);
  build(node * 2 + 1);
}
long long cnt = 0;
void dfs(long long i, long long sum) {
  if (visited[i]) {
    return;
  }
  visited[i] = true;
  if (adj[i].size() == 0) {
    weight[cnt] = sum;
    cnt++;
  }
  for (long long j = 0; j < adj[i].size(); j++) {
    dfs(adj[i][j].first, sum + adj[i][j].second);
  }
}
long long t[2000000];
long long answer = 0;
void build2(long long node, long long n) {
  long long x, y;
  for (long long int t = (1 << n) - 1; t >= 1; t--) {
    x = a[t * 2];
    y = a[2 * t + 1];
    a[t] += max(x, y);
    answer += 2 * max(x, y) - x - y;
  }
}
int main() {
  long long n, pow[13], i, j, sum, res;
  cin >> n;
  pow[0] = 1;
  for (i = 1; i <= n + 1; i++) {
    pow[i] = pow[i - 1] * 2;
  }
  nodes = pow[n + 1] - 1;
  edge = nodes - 1;
  for (i = 2; i <= nodes; i++) {
    cin >> a[i];
  }
  build(1);
  dfs(1, 0);
  cnt--;
  build2(1, n);
  cout << answer;
  return 0;
}
