#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 11) + 2;
int height[N];
int light[N];
int tmp[N];
int main() {
  int n;
  cin >> n;
  for (int i = 2; i <= (1 << (n + 1)) - 1; ++i) {
    cin >> light[i];
  }
  for (int i = (1 << (n + 1)) - 1; i >= 2; --i) {
    height[i / 2] = max(height[i / 2], height[i] + light[i]);
  }
  long long ans = 0;
  for (int i = 1; i <= (1 << n) - 1; ++i) {
    int left = 2 * i;
    int right = 2 * i + 1;
    int rem_left = height[1] - height[left] - light[left] - tmp[i];
    tmp[left] = light[left] + tmp[i] + rem_left;
    int rem_right = height[1] - height[right] - light[right] - tmp[i];
    tmp[right] = light[right] + tmp[i] + rem_right;
    ans += rem_left + rem_right;
  }
  cout << ans << endl;
}
