import Control.Monad
import Data.List
g p s=let q=(takeWhile(<p)s) in sort q++drop(length q)s
f an sp n=if null sp then sum(an) else let p=head(head sp);q=last(head sp) in f (g p (map(\x->if q>x then q else x)(take p an)++(drop p an))) (init sp) p
main=do
 [n,m]<-map read.words<$>getLine
 an<-map read.words<$>getLine
 bc<-replicateM m (map read.words<$>getLine)
 let sp=sortBy(\x y->let p=last x;q=last y in if p>q then GT else if p==q then EQ else LT)bc
     sa=sort an
 print$f sa sp 0
 