module Main where

import Data.List
import Control.Monad
import Debug.Trace
import qualified Data.ByteString.Char8 as BC
import Data.Maybe (fromJust, catMaybes)
import qualified Data.Vector as V


readTuple2 :: IO (Int, Int)
readTuple2 = do
    [a, b] <- map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine
    return (a, b)

readTuple3 :: IO (Int, Int, Int)
readTuple3 = do
    [a, b, c] <- map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine
    return (a, b, c)

readInt :: IO Int
readInt = fst . fromJust . BC.readInt <$> BC.getLine

readInts :: IO [Int]
readInts = do
    strs <- BC.words  <$> BC.getLine
    let bcList = catMaybes $ (BC.readInt <$> strs)
    pure $ fst <$> bcList

onOffLists :: Int -> Int -> [[Int]]
onOffLists i m = if i == m then [[0], [1]] else
    let
        prevLists = onOffLists (i + 1) m
    in
        [(1:), (0:)] <*> prevLists

checkVector :: [([Int], Int)] -> V.Vector Int -> Bool
checkVector ss v = all checker ss where
    checker (s,p) = ((foldr (\si acc -> (v V.! (si - 1) ) + acc ) 0 s) `mod` 2) == p

split :: (a -> Bool) -> [a] -> [[a]]
split f [] = []
split f as = let
    (fo, la) = span f as
    las = case la of
        [] -> []
        xs -> split f $ tail xs
    in
        case fo of
            [] -> las
            xs -> xs : las

solve :: [Int] -> Int
solve [] = 0
solve hs = let
    lowest = minimum hs
    waterGiven = fmap (\a -> a - lowest) hs
    newGroups = split (/=0) waterGiven
    in
        lowest + (sum $ solve <$> newGroups)


main = do
    _ <- getLine
    hs <- readInts
    print $ solve hs

