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

nested_out_bc = c_sorted_bcs.inject([]) do|memo, bc|
  memo.concat(Array.new(bc[0], bc[1]))
  memo
end

border_index = as.zip(nested_out_bc).map.with_index {|a_c, index|
  a_c[0] && a_c[1] && a_c[0] > a_c[1] ? index : nil
}.compact.first

as[0..border_index-1] = nested_out_bc[0..border_index-1] if border_index

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