solve :: [Int] -> Int -> [Int]
solve [] prev = [prev]
solve (b:bs) prev = prev' : solve bs b
  where prev' = if prev <= b then prev else b

main :: IO ()
main = do
  getLine
  bs <- reverse . map read . words <$> getLine
  print . sum . solve bs $ head bs