#include <bits/stdc++.h>
const int mod = ((1E9) + 7);
const int intmax = ((1E9) + 7);
using namespace std;
int adj[2050][2050];
int count_ = 0;
int l, r;
int dfs(int node) {
  if (node >= l && node <= r) {
    return 0;
  }
  int res1 = dfs(2 * node);
  int res2 = dfs(2 * node + 1);
  res1 += adj[node][2 * node];
  res2 += adj[node][2 * node + 1];
  ;
  int diff(abs(res1 - res2));
  count_ += diff;
  return max(res1, res2);
}
int main() {
  int test, a, b, c, d, n;
  scanf("%d", &n);
  int m = (1 << (n + 1)) - 2;
  l = (1 << n);
  r = (1 << (n + 1)) - 1;
  for (int i = 2; i <= m + 1; i++) {
    scanf("%d", &a);
    adj[i][i / 2] = a;
    adj[i / 2][i] = a;
  }
  dfs(1);
  cout << count_;
  return 0;
}
