module Memoizable
	def memoize(name)
		original = "__original_#{name}__"
		([Class, Module].include?(self.class) ? self : self.class).class_eval {
			alias_method(original, name)
			private(original)
			cache = {}
			define_method(name){|*args|
				cache[args] ||= send(original, *args)
			}
		}
	end 
end

include Memoizable

m, n = gets.split.map &:to_i

$manju = (1..m).map { gets.to_i }
$manju.sort!.reverse!

$box = (1..n).map { gets.split.map &:to_i }
$box.sort_by!{|capacity, price| -capacity / price.to_f}

def f(i, j)
	return 0 if i >= $manju.size || j >= $box.size

	capacity, price = $box[j]
	return 0 if $manju[i] <= price / capacity.to_f

	n = [capacity, $manju.size - i].min
	used_case = $manju[i..i+n-1].inject(:+) - price + f(i+n, j+1)
	unused_case = f(i, j+1)

	[used_case, unused_case].max
end

memoize :f

p f(0, 0)