import Data.List

main = print . solve . map read . drop 1 . words =<< getContents

solve :: [Int] -> Int
solve (x:as) = fst . foldl' (\(t,b) c -> let c' = max 0 (c-b) in (t+c',c')) (0,0) . zipWith (\p n -> max 0 $ p+n-x) as $ drop 1 as
