N,M=gets.split.map(&:to_i)
as=gets.split.map(&:to_i)
cs,bs=M.times.map{gets.split.map(&:to_i)}.transpose
card=Hash.new{0}
as.each do |e|
  card[e]+=1
end
M.times do |i|
  card[bs[i]]+=cs[i]
end

cnt=0
pos=0
key=card.keys.sort.reverse
last=Array.new
while cnt<=N
  last+=Array.new(card[key[pos]],key[pos])
  cnt += card[key[pos]]
  pos+=1
end
puts last[0,N].inject(:+)
