(N, M), *AB = [list(map(int, s.split())) for s in open(0)]
A = sorted(AB[0])
D = []
for b, c in sorted(AB[1:], key=lambda x: (-x[1], x[0])):
    D.extend([c] * b)
    if len(D) >= N:
        D = D[:N]
        break
D = D[::-1]
for i in range(N):
    if not D or D[-1] <= A[i]:
        break
    A[i] = D.pop()

print(sum(A)) list(map(int, s.split())) for s in open(0) for s in open(0) s open(0) open 0 for s in open(0) list(map(int, s.split())) list map(int, s.split()) map int s.split() s.split s split (N, M), *AB = [list(map(int, s.split())) for s in open(0)] (N, M) N M [list(map(int, s.split())) for s in open(0)] *AB AB A = sorted(AB[0]) A sorted(AB[0]) sorted AB[0] AB 0 D = [] D [] for b, c in sorted(AB[1:], key=lambda x: (-x[1], x[0])):
    D.extend([c] * b)
    if len(D) >= N:
        D = D[:N]
        break b c sorted(AB[1:], key=lambda x: (-x[1], x[0])) sorted AB[1:] AB 1: 1 key=lambda x: (-x[1], x[0]) key lambda x: (-x[1], x[0]) (-x[1], x[0]) -x[1] x[1] x 1 x[0] x 0 x D.extend([c] * b) D.extend D extend [c] * b [c] c b if len(D) >= N:
        D = D[:N]
        break len(D) >= N len(D) len D N D = D[:N] D D[:N] D :N N break D = D[::-1] D D[::-1] D ::-1 -1 for i in range(N):
    if not D or D[-1] <= A[i]:
        break
    A[i] = D.pop() i range(N) range N if not D or D[-1] <= A[i]:
        break
     not D or D[-1] <= A[i] not D D D[-1] <= A[i] D[-1] D -1 A[i] A i break A[i] = D.pop() A[i] A i D.pop() D.pop D pop print(sum(A)) print sum(A) sum A D = D[::-1] D[::-1] D A = sorted(AB[0]) sorted(AB[0]) A D = [] [] D D = D[:N] D[:N] D
(N, M), *AB = [list(map(int, s.split())) for s in open(0)]
A = sorted(AB[0])
D = []
for b, c in sorted(AB[1:], key=lambda x: (-x[1], x[0])):
    D.extend([c] * b)
    if len(D) >= N:
        D = D[:N]
        break
D = D[::-1]
for i in range(N):
    if not D or D[-1] <= A[i]:
        break
    A[i] = D.pop()

print(sum(A))