lines = readlines.map(&:strip).map{|line| line.split.map(&:to_i)}

n, m = lines.shift
as = lines.shift.sort
bcs = lines

c_sorted_bcs = bcs.sort{|l,r|l[1]<=>r[1]}.reverse


#puts c_sorted_bcs.inspect
#puts as.inspect

#puts '###'
c_sorted_bcs.each do|b, c|
  current_targets = as.select.with_index{|a,i| c>a && i<b}
  as.shift(current_targets.size)
  as.concat(Array.new(current_targets.size, c))
  as = as.sort
#  puts current_targets.inspect
#  puts as.inspect
#  puts '---'
end


puts as.inject(0){|m,n|m+n}
