local n = io.read("*n")
local t = {}
local imos = {}
for i = 1, n + 1 do
  t[i] = io.read("*n")
  imos[i] = 0
end
imos[n + 2] = 0
local rem = 1
local score = 0
local stage = 0
for i = 1, n + 1 do
  local v = t[i]
  while rem < v and stage < i do
    score = score + rem * (i - stage)
    imos[stage + 1] = imos[stage + 1] + rem
    imos[i] = imos[i] - rem
    stage = stage + 1
    v = v - rem
    rem = imos[stage]
    imos[stage + 1] = imos[stage + 1] + imos[stage]
  end
  if i <= stage then
    score = -1
    break
  end
  score = score + v * (i - stage)
  rem = rem - v
  imos[stage + 1] = imos[stage + 1] + v
  imos[i] = imos[i] - v
end
print(score)
