Buggy python submission (verdict: Time Limit Exceeded):
import sys
def read():
    return sys.stdin.readline().rstrip()
n, m = map(int, read().split())
A = list(map(int, read().split()))
B = []
for i in range(m):
    b,c = list(map(int, read().split()))
    B.extend([c]*b)
print(sum(sorted(A+B, reverse=True)[:len(A)]))

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))

B = [[] for _ in range(m)]
for i in range(m):
    B[i] = list(map(int, input().split()))
B.sort(key=lambda x: -x[1])

_A = []
cnt = 0
for i in range(m):
    _A.extend([B[i][1]]*B[i][0])
    cnt += B[i][0]
    if cnt > n:
        break
A += _A
A.sort(reverse=True)
print(sum(A[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = list(map(int, input().split()))
B = []
for i in range(m):
    b,c = list(map(int, input().split()))
    B.extend([c]*b)
print(sum(sorted(A+B, reverse=True)[:len(A)]))

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))

B = [[] for _ in range(m)]
for i in range(m):
    B[i] = list(map(int, input().split()))
B.sort(key=lambda x: -x[1])

_A = []
cnt = 0
for i in range(m):
    _A.extend([B[i][1]]*B[i][0])
    cnt += B[i][0]
    if cnt > n:
        break
A += _A
A.sort(reverse=True)
print(sum(A[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = list(map(int, input().split()))
B = []
for i in range(m):
    b,c = list(map(int, input().split()))
    B.extend([c]*b)
print(sum(sorted(A+B, reverse=True)[:len(A)]))

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))

B = [[] for _ in range(m)]
for i in range(m):
    B[i] = list(map(int, input().split()))
B.sort(key=lambda x: -x[1])

_A = []
cnt = 0
for i in range(m):
    _A.extend([B[i][1]]*B[i][0])
    cnt += B[i][0]
    if cnt > n:
        break
A += _A
A.sort(reverse=True)
print(sum(A[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n, m = map(int, input().split())
A = list(map(int, input().split()))
cA = Counter(A)
mA = min(cA)
B = [[] for _ in range(m)]
for i in range(m):
    B[i] = list(map(int, input().split()))
B.sort(reverse=True, key=lambda x: x[1])

for (b,c) in B:
    if c < mA: break
    while b>0 and mA<c:
        if b>cA[mA]:
            cA[c] += cA[mA]; b -= cA[mA]
            del cA[mA]
            mA = min(cA)
        else:
            cA[mA] -= b
            cA[c] += b
            b = 0

ans = 0
for key, val in cA.items():
    ans += key*val
print(ans)

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))

B = [[] for _ in range(m)]
for i in range(m):
    B[i] = list(map(int, input().split()))
B.sort(key=lambda x: -x[1])

_A = []
cnt = 0
for i in range(m):
    _A.extend([B[i][1]]*B[i][0])
    cnt += B[i][0]
    if cnt > n:
        break
A += _A
A.sort(reverse=True)
print(sum(A[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n, m = map(int, input().split())
A = list(map(int, input().split()))
cA = Counter(A)
mA = min(cA)
B = [[] for _ in range(m)]
for i in range(m):
    B[i] = list(map(int, input().split()))
B.sort(reverse=True)

for (b,c) in B:
    while b>0 and mA<c:
        if b>cA[mA]:
            cA[c] += cA[mA]; b -= cA[mA]
            del cA[mA]
            mA = min(cA)
        else:
            cA[mA] -= b
            cA[c] += b
            b = 0
ans = 0
for key, val in cA.items():
    ans += key*val
print(ans)

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))

B = [[] for _ in range(m)]
for i in range(m):
    B[i] = list(map(int, input().split()))
B.sort(key=lambda x: -x[1])

_A = []
cnt = 0
for i in range(m):
    _A.extend([B[i][1]]*B[i][0])
    cnt += B[i][0]
    if cnt > n:
        break
A += _A
A.sort(reverse=True)
print(sum(A[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n, m = map(int, input().split())
A = list(map(int, input().split()))
cA = Counter(A)
mA = min(cA)
for i in range(m):
    b, c = map(int, input().split())
    while b>0 and mA<c:
        if b>cA[mA]:
            cA[c] += cA[mA]; b -= cA[mA]
            del cA[mA]
            mA = min(min(cA), c)
        else:
            cA[mA] -= b
            cA[c] += b
            b = 0
ans = 0
for key, val in cA.items():
    ans += key*val
print(ans)

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))

B = [[] for _ in range(m)]
for i in range(m):
    B[i] = list(map(int, input().split()))
B.sort(key=lambda x: -x[1])

_A = []
cnt = 0
for i in range(m):
    _A.extend([B[i][1]]*B[i][0])
    cnt += B[i][0]
    if cnt > n:
        break
A += _A
A.sort(reverse=True)
print(sum(A[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n, m = map(int, input().split())
A = list(map(int, input().split()))
cA = Counter(A)
for i in range(m):
    b, c = map(int, input().split())
    while b>0 and min(cA)<c:
        if b>cA[min(cA)]:
            cA[c] += cA[min(cA)]; b -= cA[min(cA)]
            del cA[min(cA)]
        else:
            cA[min(cA)] -= b
            cA[c] += b
            b = 0
ans = 0
for key, val in cA.items():
    ans += key*val
print(ans)

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))

B = [[] for _ in range(m)]
for i in range(m):
    B[i] = list(map(int, input().split()))
B.sort(key=lambda x: -x[1])

_A = []
cnt = 0
for i in range(m):
    _A.extend([B[i][1]]*B[i][0])
    cnt += B[i][0]
    if cnt > n:
        break
A += _A
A.sort(reverse=True)
print(sum(A[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n, m = map(int, input().split())
A = list(map(int, input().split()))
cA = Counter(A)
for i in range(m):
    b, c = map(int, input().split())
    while b>0 and min(cA)<c:
        if b>cA[min(cA)]:
            cA[c] += cA[min(cA)]; b -= cA[min(cA)]
            del cA[min(cA)]
        else:
            cA[min(cA)] -= b
            cA[c] += b
            b = 0
ans = 0
for key, val in cA.items():
    ans += key*val
print(ans)

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))

B = [[] for _ in range(m)]
for i in range(m):
    B[i] = list(map(int, input().split()))
B.sort(key=lambda x: -x[1])

_A = []
cnt = 0
for i in range(m):
    _A.extend([B[i][1]]*B[i][0])
    cnt += B[i][0]
    if cnt > n:
        break
A += _A
A.sort(reverse=True)
print(sum(A[:n]))