#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > graph[10000];
vector<long long> dist;
long long dp[10000];
long long mx;
long long out = 0;
long long dfs(int u) {
  long long e = 0;
  for (int i = 0; i < graph[u].size(); i++) {
    e = max(e, graph[u][i].second + dfs(graph[u][i].first));
  }
  return dp[u] = e;
}
void solve(int u) {
  for (int i = 0; i < graph[u].size(); i++) {
    int add = dp[u] - dp[graph[u][i].first] - graph[u][i].second;
    out += add;
    solve(graph[u][i].first);
  }
}
int main() {
  int n;
  cin >> n;
  memset(dp, 0, sizeof dp);
  for (int i = 1; i <= (1 << (n)) - 1; i++) {
    graph[i].push_back(make_pair((i << 1), 0));
    graph[i].push_back(make_pair((i << 1) + 1, 0));
  }
  int j = 1;
  for (int i = 1; i <= (1 << (n + 1)) - 2; i += 2) {
    int x, y;
    cin >> x >> y;
    graph[j][0].second = x;
    graph[j][1].second = y;
    j++;
  }
  dfs(1);
  mx = dp[1];
  solve(1);
  cout << out << endl;
  return 0;
}
