import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.List

readInts :: IO [Int]
readInts = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

main :: IO ()
main = do
  getLine
  bs <- readInts
  print $ solve bs

solve :: [Int] -> Int
solve = sum . go . reverse
  where
    go [b] = [b, b]
    go (b1:b2:bs) = (b1 `max` b2) : go (map (`min` b1) (b2:bs))
