import Control.Monad
import Data.List
f an sp n=if null sp then sum(an) else let p=head(head sp);q=last(head sp) in f (sort((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 sa=sort an
 print$f sa bc 0
 