import qualified Data.ByteString.Char8 as C
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM

r :: Int -> IO (U.Vector Int)
r n = U.unfoldrN n (C.readInt . C.dropWhile (==' ')) <$> C.getLine

d :: Int -> Int -> Float
d x y = fromIntegral x / fromIntegral y

main :: IO ()
main = do
    n <- readLn
    as <- r (n+1)
    let minima = U.scanr1 (\nw ac -> ceiling (d ac 2) + nw) as
    if minima U.! 0 > 1 then print (-1)
        else do
            let strings = U.scanr1 (+) as

            knots <- UM.new (n+1)
            UM.write knots 0 (1 :: Int)

            generateKnots as strings knots 1

            finalKnots <- U.freeze knots
            print $ U.sum finalKnots

generateKnots :: U.Vector Int -> U.Vector Int -> UM.IOVector Int -> Int -> IO ()
generateKnots leafs strings acc cur -- assuming cur >= 1
    | U.length leafs <= cur = return ()
    | otherwise = do
    parentNodes <- UM.read acc (cur-1)
    let parentLeafs = leafs U.! (cur-1)

    let parentNonLeafs = parentNodes - parentLeafs
    let currentNodes = min (strings U.! cur) (2 * parentNonLeafs)

    UM.write acc cur currentNodes

    generateKnots leafs strings acc (cur+1)