#include <bits/stdc++.h>
using namespace std;
int n;
long long int arr[10000];
vector<long long int> v;
void dfs(int start, vector<int> &vis, long long cost, long long lvl) {
  vis[start] = 1;
  if (lvl == n) v.push_back(cost);
  int go1 = 2 * start, go2 = 2 * start + 1;
  if (go1 >= (1 << (n + 1))) return;
  dfs(go1, vis, cost + arr[go1], lvl + 1);
  dfs(go2, vis, cost + arr[go2], lvl + 1);
}
int main() {
  cin >> n;
  int last = (1 << (n + 1)) - 1;
  for (int i = 2; i < (1 << (n + 1)); i++) cin >> arr[i];
  vector<int> vis(10000, 0);
  int lvl = 0;
  long long cost = 0;
  long long ans = 0;
  lvl = n;
  while (lvl) {
    if (lvl == n) {
      for (int i = 0; i < (1 << lvl); i += 2) {
        long long int bi = max(arr[last - i], arr[last - i - 1]);
        long long int sm = min(arr[last - i], arr[last - i - 1]);
        ans += bi - sm;
        arr[last - i] = arr[last - i - 1] = bi;
      }
      last -= (1 << lvl);
      lvl--;
    } else {
      for (int i = 0; i < (1 << lvl); i += 2) {
        int now1 = last - i, now2 = last - i - 1;
        long long p1 = arr[2 * now1], p2 = arr[2 * now2];
        p1 += arr[now1];
        p2 += arr[now2];
        long long dif = abs(p1 - p2);
        ans += dif;
        arr[last - i] = arr[last - i - 1] = max(p1, p2);
      }
      last -= (1 << lvl);
      lvl--;
    }
  }
  cout << ans << endl;
  return 0;
}
