import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    getLine
    as <- getIntList
    let ans = fst $ foldl' f (0,0) as
    print ans

f (s,h1) h2 | h1 <= h2 = (s,h2)
            | otherwise = (s + h1 - h2, h1)
