{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
import qualified Data.ByteString.Char8 as B
import           Data.Char             (isSpace)
import qualified Data.Vector.Unboxed   as V

main :: IO ()
main = do
  n <- readLn
  hs <- V.unfoldrN n parseInt <$> B.getLine
  print $ solve n hs

solve :: Int -> V.Vector Int -> Int
solve _ hs = solve' hs

solve' :: V.Vector Int -> Int
solve' hs
  | V.length hs == 0 = 0
  | otherwise  = h + solve' (V.take i hs') + solve' (V.drop (i + 1) hs')
  where
    i = V.minIndex hs
    h = hs V.! i
    hs' = V.map (subtract h) hs

parseInt :: B.ByteString -> Maybe (Int, B.ByteString)
parseInt = B.readInt . B.dropWhile isSpace
