#include <bits/stdc++.h>
using namespace std;
int ans[100000] = {};
pair<int, int> r[100000] = {};
int main() {
  int n = ({
    int x;
    scanf("%d", &x);
    x;
  });
  for (int i = (0); i < ((1 << (n + 1)) - 2); i += (1)) {
    if (i % 2 == 0) {
      r[(i + 2) / 2].first = ({
        int x;
        scanf("%d", &x);
        x;
      });
    } else {
      r[(i + 2) / 2].second = ({
        int x;
        scanf("%d", &x);
        x;
      });
    }
  }
  int res = 0;
  for (int i = ((1 << n) - 1); i >= (1); i--) {
    int diff = abs((r[i].first + ans[2 * i]) - (r[i].second + ans[2 * i + 1]));
    res += diff;
    ans[i] = max(r[i].first + ans[2 * i], r[i].second + ans[2 * i + 1]);
  }
  cout << res;
}
