{-# LANGUAGE FlexibleContexts #-}

import Control.Monad
import Control.Monad.State
import Control.Applicative
import Data.List
import Data.Array
import Data.Maybe
import Data.Int
import qualified Data.ByteString.Char8 as BS
import qualified Data.Set as Set

readIntBS = fst . fromJust . BS.readInt

infixr 1 ?
(?) :: Bool -> (a,a) -> a
(?) b t = (if b then fst else snd) t

main :: IO ()
main = do
  n <- readLn :: IO Int64
  xs <- sort . (flip zip) [1..] . map (fromIntegral . readIntBS) . BS.words <$> BS.getLine :: IO [(Int64,Int64)]

  let
    aux [] = return 0
    aux ((a,i):xs) = do
      (Just l) <- Set.lookupLT i <$> get
      (Just r) <- Set.lookupGT i <$> get
      modify $ Set.insert i
      ((i-l)*(r-i)*a+) <$> aux xs

  print $ evalState (aux xs) (Set.fromList [0,n+1])
  