gets
aaa = gets.split.map(&:to_i).sort # .map{|aa|[aa, false]} # 書き換えたか？
xxx = []
# p aaa
while g = gets&.split&.map(&:to_i)
  g[0].times do
    xxx << g[1]
  end
end
xxx.sort_by!(&:-@)

sum = 0
[aaa.size, xxx.size].max.times do |i|
  sum += [aaa[i] || 0, xxx[i] || 0].max
end
puts sum
# # p xxx
# count = 0
# xxx.size.times do |i|
#   count += 1 if(aaa[i] && aaa[i] < xxx[i])
# end
# p count
# p xxx[0..(count - 1)].inject(:+) + aaa[count..-1].inject(:+)
# xxx.size.times do |i|
#   if aaa[i] && aaa[i] < xxx[i]
#     aaa[i] = xxx[i]
#   end
# end
#
# sum = 0
# aaa.each do |(b, _c)|
#   sum += b
# end
# p sum
