#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> v[5000];
int vis[5000];
int cnt;
int dfs(int n) {
  vis[n] = 1;
  int mx = 0, d;
  vector<int> res;
  for (int i = 0; i < v[n].size(); i++) {
    if (!vis[v[n][i].first]) {
      d = dfs(v[n][i].first) + v[n][i].second;
      mx = max(d, mx);
      res.push_back(d);
    }
  }
  for (auto x : res) {
    cnt += abs(mx - x);
  }
  return mx;
}
void test_case() {
  int k;
  cin >> k;
  int n = pow(2, k + 1) - 2;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    int t = i + 1;
    int u = t / 2;
    v[u].push_back({t, x});
    v[t].push_back({u, x});
  }
  int tmp = dfs(1);
  cout << cnt << "\n";
}
int main() {
  int t = 1;
  for (int i = 0; i < t; i++) {
    test_case();
  }
}
