import Control.Applicative

main :: IO ()
main = getLine >> solve <$> f >>= print
  where
    f = map read <$> words <$> getLine

solve :: [Int] -> Int
solve xs = f 0 xs
  where
    f h [x] = h + x
    f h (x:y:ys)
      | x > y = f (h + x - y) (y:ys)
      | otherwise = f h (y:ys)
