import Data.List
main = do
  _<-getLine
  a <- map (read::String->Int) . words <$> getLine
  print $ sum $ zipWith (-) (step a []) a
  
step [] result = reverse result
step (x:xs) [] = step xs (x:[])
step (x:xs) (y:ys) = step xs ((max x y):y:ys)