readInts :: IO [Int]
readInts = map read . words <$> getLine

main :: IO ()
main = do
  n <- readLn
  as <- readInts
  print $ solve n as

solve :: Int -> [Int] -> Int
solve n as = go 0 0 as
 where
  go acc h0 [] = acc
  go acc h0 (a:as)
    | h0 <= a   = go acc (max h0 a) as
    | otherwise = go (acc + h0 - a) h0 as
