#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n + 5);
  for (int i = 1; i <= n; i++) cin >> a[i];
  int tot = accumulate(a.begin(), a.end(), 0);
  if ((n <= 2 && tot > 0) || (n % 2 == 0 && a[n] > 0)) {
    cout << -1 << endl;
    return;
  }
  int ans = 0;
  auto check = [&](int i) {
    int hf = n / 2;
    if (a[2 * i] > 0) return 1;
    if (a[2 * i + 1] > 0) return 1;
    if (i == 1 && a[i] > 0) return 1;
    return 0;
  };
  auto take = [&](int i) {
    a[i] = max(a[i] - 1, 0);
    a[2 * i] = max(a[2 * i] - 1, 0);
    a[2 * i + 1] = max(a[2 * i + 1] - 1, 0);
    return;
  };
  const int inf = INT_MAX - 100;
  auto go = [&]() {
    int cnt = 0, pos = 0;
    for (int i = n / 2; i >= 1; i--) {
      if (check(i)) {
        cnt = (a[i] > 0) + (a[2 * i] > 0) + (a[2 * i + 1] > 0);
        pos = i;
        break;
      }
    }
    take(pos);
    return cnt;
  };
  auto print = [&]() {
    for (int i = 1; i <= n; i++) cout << a[i] << ' ';
    cout << endl;
  };
  while (tot > 0) {
    tot -= go();
    ans++;
  }
  cout << ans << '\n';
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
  return 0;
}
