
from collections import Counter
from heapq import heappop, heappush

N, M = map(int, input().split())
A = list(map(int, input().split()))
X = [list(map(int, input().split())) for _ in range(M)]

pq = []
ctr = Counter(A)
for val, cnt in ctr.items():
    heappush(pq, (-val, cnt))

for b, c in X:
    heappush(pq, (-c, b))

cnt = 0
ans = 0
while cnt < N:
    val, c = heappop(pq)
    ans += val * min(c, N - cnt)
    cnt += c

print(-ans) from collections import Counter from heapq import heappop, heappush N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M A = list(map(int, input().split())) A list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split list(map(int, input().split())) for _ in range(M) for _ in range(M) _ range(M) range M for _ in range(M) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split X = [list(map(int, input().split())) for _ in range(M)] X [list(map(int, input().split())) for _ in range(M)] pq = [] pq [] ctr = Counter(A) ctr Counter(A) Counter A for val, cnt in ctr.items():
    heappush(pq, (-val, cnt)) val cnt ctr.items() ctr.items ctr items heappush(pq, (-val, cnt)) heappush pq (-val, cnt) -val val cnt for b, c in X:
    heappush(pq, (-c, b)) b c X heappush(pq, (-c, b)) heappush pq (-c, b) -c c b cnt = 0 cnt 0 ans = 0 ans 0 while cnt < N:
    val, c = heappop(pq)
    ans += val * min(c, N - cnt)
    cnt += c cnt < N cnt N val, c = heappop(pq) val heappop(pq) heappop pq c ans += val * min(c, N - cnt) ans val * min(c, N - cnt) val min(c, N - cnt) min c N - cnt N cnt cnt += c cnt c print(-ans) print -ans ans cnt += c c cnt N, M = map(int, input().split()) map(int, input().split()) N ctr = Counter(A) Counter(A) ctr val, c = heappop(pq) heappop(pq) val X = [list(map(int, input().split())) for _ in range(M)] [list(map(int, input().split())) for _ in range(M)] X pq = [] [] pq ans += val * min(c, N - cnt) val * min(c, N - cnt) ans A = list(map(int, input().split())) list(map(int, input().split())) A M = map(int, input().split()) map(int, input().split()) M ans = 0 0 ans cnt = 0 0 cnt c = heappop(pq) heappop(pq) c

from collections import Counter
from heapq import heappop, heappush

N, M = map(int, input().split())
A = list(map(int, input().split()))
X = [list(map(int, input().split())) for _ in range(M)]

pq = []
ctr = Counter(A)
for val, cnt in ctr.items():
    heappush(pq, (-val, cnt))

for b, c in X:
    heappush(pq, (-c, b))

cnt = 0
ans = 0
while cnt < N:
    val, c = heappop(pq)
    ans += val * min(c, N - cnt)
    cnt += c

print(-ans)
