#include <bits/stdc++.h>
using namespace std;
const int kInf = 1000 * 1000 * 1000;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  int NN = (1 << (N + 1));
  vector<int> A(NN);
  for (int i = 2; i < NN; ++i) {
    cin >> A[i];
  }
  vector<vector<int>> deps(NN);
  vector<int> ways;
  int lim = 1 << (N + 1);
  for (int from = 1 << N; from < lim; ++from) {
    int sum = 0;
    for (int i = from; i > 1; i /= 2) {
      deps[i].push_back(ways.size());
      sum += A[i];
    }
    ways.push_back(sum);
  }
  int mx = *max_element(ways.begin(), ways.end());
  int64_t result = 0;
  for (int i = 2; i < NN; ++i) {
    int can_add = kInf;
    for (int d : deps[i]) {
      can_add = min(can_add, mx - ways[d]);
    }
    if (can_add != kInf) {
      result += can_add;
      for (int d : deps[i]) {
        ways[d] += can_add;
      }
    }
  }
  cout << result << '\n';
  return 0;
}
