n, m = gets.split.map(&:to_i)
as = gets.split.map(&:to_i).sort
p(as)

xs = Array.new(m).map { gets.split.map(&:to_i) }

xs = xs.sort.group_by {|x| x[1]}.map {|k,v| [k, [v.map {|ys| ys[0]}.inject(:+), n].min]}.reverse

cs = []
s = n
bs = as
xs.each do |c, k|
  k = [k, s].min
  r = (0...k).find(Proc.new {k}) { |i| bs[i] >= c }
  s = (r...s).find(Proc.new {s}) { |i| bs[i] >= c }
  cs.push(bs[s..-1])
  bs = bs[r...s] + Array.new(r, c)
end

p(cs.map { |ds| ds.inject(:+) }.inject(:+))
