Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(reverse=True, key=lambda x: x[1])

for i in range(min(n, m)):
    a.extend([bc[i][1]]*bc[i][0])

a.sort(reverse=True)
print(sum(a[:n]))


Fixed submission:
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(reverse=True, key=lambda x: x[1])

bcs = []
for b, c in bc:
    if len(bcs) > n: break
    bcs.extend([c]*b)

a.extend(bcs)
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))

for i in range(min(n, m)):
    b, c = map(int, input().split())
    a.extend([c]*b)

a.sort(reverse=True)

print(sum(a[:n]))


Fixed submission:
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(reverse=True, key=lambda x: x[1])

bcs = []
for b, c in bc:
    if len(bcs) > n: break
    bcs.extend([c]*b)

a.extend(bcs)
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()))

for _ in range(m):
    b, c = map(int, input().split())
    a += [c]*b

a.sort(reverse=True)

print(sum(a[:n]))


Fixed submission:
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(reverse=True, key=lambda x: x[1])

bcs = []
for b, c in bc:
    if len(bcs) > n: break
    bcs.extend([c]*b)

a.extend(bcs)
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

q = []
for _ in range(m):
    b, c = map(int, input().split())
    q += [-c]*b

heapq.heapify(q)

for i in range(n):
    if not q: break
    x = -heapq.heappop(q)
    if a[i] < x:
        a[i] = x
    else: break

print(sum(a))


Fixed submission:
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(reverse=True, key=lambda x: x[1])

bcs = []
for b, c in bc:
    if len(bcs) > n: break
    bcs.extend([c]*b)

a.extend(bcs)
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()))

bc = []
for _ in range(m):
    b, c = map(int, input().split())
    bc += [c]*b

a.sort()
bc.sort(reverse=True)

for i in range(min(len(bc), n)):
    if a[i] < bc[i]:
        a[i] = bc[i]
    else: break

print(sum(a))


Fixed submission:
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(reverse=True, key=lambda x: x[1])

bcs = []
for b, c in bc:
    if len(bcs) > n: break
    bcs.extend([c]*b)

a.extend(bcs)
a.sort(reverse=True)
print(sum(a[:n]))