{-# LANGUAGE BangPatterns #-}
import Control.Arrow
import Control.Monad.Fix
import Data.Char
import Data.Int
import Data.Foldable
import Data.List
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as B
import Debug.Trace

solve :: Int -> Int -> VU.Vector Int -> VU.Vector (Int, Int) -> Int
solve !n !m !an !bcs = (\(acc, an) -> acc + sum an) $ foldr
  ( \(!b, !c) (!acc, !an) -> if null an
    then (acc, an)
    else
      let (an1 , an2 ) = splitAt b an
          (an1t, an1d) = span (< c) an1
          an'          = an1d ++ an2
          isFin        = null an' || head an' >= c
      in  ( acc + length an1t * c + if isFin then sum an' else 0
          , if isFin then [] else an'
          )
  )
  (0, an')
  bcs'
 where
  an' = sort $ VU.toList an

  bcs' =
    sortBy (\(b1, c1) (b2, c2) -> compare (c1, b1) (c2, b2)) $ VU.toList bcs

main = do
  let readInt :: Integral a => B.ByteString -> Maybe (a, B.ByteString)
      readInt = fmap (first fromIntegral . second B.tail) . B.readInt

  (n, m) <-
    (\vec -> (vec VU.! 0, vec VU.! 1)) . VU.unfoldrN 2 readInt <$> B.getLine
  an  <- VU.unfoldrN n readInt <$> B.getLine
  bcs <-
    VU.replicateM m
    $   (\vec -> (vec VU.! 0, vec VU.! 1))
    .   VU.unfoldrN 2 readInt
    <$> B.getLine
  print $ solve n m an bcs
