#include <bits/stdc++.h>
using namespace std;
int a[110];
int n;
int dp[110][1010];
int r(int v, int how) {
  if (dp[v][how] != -1) return dp[v][how];
  int now = ((0 < a[v] - how) ? (a[v] - how) : (0));
  int nv = v + 1;
  if (nv * 2 + 1 > n) {
    if (now > 0) return dp[v][how] = 1000000000;
    if ((nv * 2 == n) && (a[n - 1])) return 1000000000;
    return dp[v][how] = 0;
  }
  int res = 1000000000;
  for (int i = (now); i < (1001); ++i) {
    int v1 = r(nv * 2 - 1, i);
    if (v1 >= 1000000000) continue;
    int v2 = r(nv * 2, i);
    if (v2 >= 1000000000) continue;
    res = min(res, v1 + v2 + i);
  }
  return dp[v][how] = res;
}
int main() {
  cin >> n;
  for (int i = (0); i < (n); ++i) cin >> a[i];
  memset(dp, -1, sizeof(dp));
  int res = r(0, 0);
  if (res >= 1000000000) res = -1;
  cout << res << endl;
  return 0;
}
