import Data.List

main=do
    line<-getLine
    line<-getLine
    let p=sort [read x::Int|x<-words line]
    ctx<-getContents
    let r=[(read (head $words c)::Int,read (last $words c)::Int)|c<-lines ctx]
    print $ sum $ merge p (toLst $ tpSort r)

tpSort :: [(Int,Int)]->[(Int,Int)]
tpSort [] = []
tpSort (x:xs)=tpSort greaterArr ++ [x] ++ tpSort lessArr
    where   lessArr = [c|c<-xs,snd c< snd x]
            greaterArr =[c|c<-xs,snd c >=snd x]


toLst :: [(Int,Int)]->[Int]
toLst [] =[]
toLst (x:xs) = replicate (fst x) (snd x) ++ toLst xs

merge :: [Int]->[Int]->[Int]
merge [] _ = []
merge xs [] = xs
merge (x:xs) (y:ys)
    | y>x = y : merge xs ys
    | otherwise = x:xs
