scan :: [Int] -> Int -> Int
scan xs = iter 0 where
    iter s e
        | e - s == 1 = e
        | check m xs = iter s m
        | otherwise  = iter m e
        where m = div (s+e) 2
    check m = iter [] 0 where
        iter ps _ [] = True
        iter ps pl (x:xs)
            | pl <  x = iter (take (x -pl) (repeat 0) ++ ps) x xs
--            | pl <  x = trace ("tr0 m:" ++ show m ++ " ns:" ++ show (reverse (take (x -pl) (repeat 0) ++ ps))) $  iter (take (x -pl) (repeat 0) ++ ps) x xs
            | otherwise = case next (drop (pl - x) ps) of
                Just ns -> iter ns x xs
--                Just ns -> trace ("tr1 m:" ++ show m ++ " ns:" ++ show (reverse ns)) $ iter ns x xs
                Nothing -> False
            where
                next [] = Nothing
                next (x:xs)
                    | m - 1 > x = Just ((x+1):xs)
                    | otherwise = fmap (0:) (next xs)

readData :: IO [Int]
readData = do
    line1 <- getLine
    let xs = map (\x -> read x :: Int) (words line1)
    return xs

main :: IO() 
main = do
    n  <- readLn :: IO Int
    la <- readData
    let ans = scan (take n la) n
    print ans