#include <bits/stdc++.h>
using namespace std;
int n, a[111], ans;
void go(int x) {
  if (x * 2 + 1 > n) return;
  go(x * 2);
  go(x * 2 + 1);
  ans += max(a[x * 2], a[x * 2 + 1]);
  a[x] = max(0, a[x] - max(a[x * 2], a[x * 2 + 1]));
  a[x * 2] = a[x * 2 + 1] = 0;
  if (x == 1) ans += a[x], a[x] = 0;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  go(1);
  for (int i = 1; i <= n; i++)
    if (a[i]) {
      puts("-1");
      return 0;
    }
  cout << ans << endl;
}
