#include <bits/stdc++.h>
using namespace std;
long long int power(int x, int y) { return pow(x, y); }
int a[1000009];
int parent[100009];
int adjust[100009];
void fun(int n) {
  int to = power(2, n + 1) - 1;
  for (int i = 1; i <= to; i++) {
    parent[i] = (int)i / 2;
  }
  for (int i = 1; i <= to; i++) {
    a[i] += a[parent[i]];
  }
}
int main(int argc, char const *argv[]) {
  long long int n;
  cin >> n;
  int to = power(2, n + 1) - 2;
  for (int i = 2; i <= to + 1; i++) {
    cin >> a[i];
  }
  fun(n);
  int maxi = -1;
  int st = power(2, n);
  int ed = power(2, n + 1) - 1;
  for (int i = st; i <= ed; i++) {
    maxi = max(maxi, a[i]);
  }
  for (int i = st; i <= ed; i++) {
    adjust[i] = maxi - a[i];
  }
  long long int ans = 0;
  for (int i = n; i > 1; i--) {
    st = power(2, i);
    ed = power(2, i + 1) - 1;
    for (int i = st; i < ed; i += 2) {
      int mini = min(adjust[i], adjust[i + 1]);
      adjust[parent[i]] = mini;
      ans += abs(adjust[i] - adjust[i + 1]);
    }
  }
  ans += (adjust[2] + adjust[3]);
  cout << ans << "\n";
  return 0;
}
