#include <bits/stdc++.h>
using namespace std;
int n, a[105];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (int i = n; i >= 1; i--) {
    if (2 * i + 1 <= n) {
      while (a[2 * i] > 0 || a[2 * i + 1] > 0) {
        a[i] = max(0, a[i] - 1);
        a[2 * i] = max(0, a[2 * i] - 1);
        a[2 * i + 1] = max(0, a[2 * i + 1] - 1);
        ans++;
      }
    }
  }
  while (3 <= n && a[1] > 0) {
    a[1] = max(0, a[1] - 1);
    a[2] = max(0, a[2] - 1);
    a[3] = max(0, a[3] - 1);
    ans++;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] != 0) ans = -1;
  }
  cout << ans << '\n';
  return 0;
}
