#include <bits/stdc++.h>
using namespace std;
vector<int> amount;
vector<vector<pair<int, int>>> v;
vector<int> MAX;
int ABS(int val) { return val < 0 ? -val : val; }
int last = 0;
void dfs(int node, int am) {
  if (v[node].size() == 0) {
    amount.push_back(am);
    return;
  }
  for (int i = 0; i < v[node].size(); ++i)
    dfs(v[node][i].first, am + v[node][i].second);
}
void dfs2(int node, int am) {
  if (v[node].size() == 0) {
    MAX[node] = last - am;
    return;
  }
  int MAXI = 2e9, MINI = 2e9;
  for (int i = 0; i < v[node].size(); ++i) {
    dfs2(v[node][i].first, am + v[node][i].second);
    MAXI = min(MAX[v[node][i].first], MAXI);
  }
  MAX[node] = MAXI;
  return;
}
void dfs3(int node, int am) {
  if (v[node].size() == 0) {
    return;
  }
  if (am == last) return;
  v[node][0].second += ABS((MAX[v[node][0].first]) - am);
  v[node][1].second += ABS((MAX[v[node][1].first]) - am);
  dfs3(v[node][0].first, am + (MAX[v[node][0].first]) - am);
  dfs3(v[node][1].first, am + (MAX[v[node][1].first]) - am);
}
int main() {
  int n;
  cin >> n;
  vector<int> g((int)(pow(2, n + 1) - 2));
  for (int i = 0; i < (int)(pow(2, n + 1) - 2); ++i) cin >> g[i];
  v.resize((int)(pow(2, n + 1) - 1) + 1);
  int j = 0;
  int sum1 = 0;
  for (int i = 1; j < g.size(); ++i) {
    sum1 += g[j];
    sum1 += g[j + 1];
    v[i].push_back(make_pair(i * 2, g[j++]));
    v[i].push_back(make_pair(i * 2 + 1, g[j++]));
  }
  dfs(1, 0);
  sort(amount.begin(), amount.end());
  last = amount[amount.size() - 1];
  MAX.resize((int)(pow(2, n + 2)));
  dfs2(1, 0);
  dfs3(1, 0);
  int sum = 0;
  for (vector<pair<int, int>> p : v)
    for (pair<int, int> y : p) {
      sum += y.second;
    }
  cout << sum - sum1 << endl;
  return 0;
}
