#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  int a[111] = {0}, n;
  cin >> n;
  for (int i = 0; i < (int)(n); i++) cin >> a[i + 1];
  int tot = 0;
  for (int i = n; i > 1; i--) {
    int t = i / 2;
    if (2 * t + 1 > n) continue;
    while (a[i]) {
      if (a[2 * t + 1]) a[2 * t + 1]--;
      if (a[2 * t]) a[2 * t]--;
      if (a[t]) a[t]--;
      tot++;
    }
  }
  if (n > 2) {
    tot += a[1];
    a[1] = 0;
  }
  int f = 0;
  for (int i = 1; i <= n; i++) f |= a[i];
  if (f)
    cout << -1 << endl;
  else
    cout << tot << endl;
  return EXIT_SUCCESS;
}
