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 = map(int, input().split())
    for j in range(b):
        B.append(c)

A.sort()
B.sort(reverse=True)
for i in range(min(N, len(B))):
    if A[i] < B[i]:
        A[i] = B[i]

ans = sum(A)
print(ans)

Fixed submission:
N, M = map(int, input().split())

A = list(map(int, input().split()))

count = 0
BC = []
for i in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))

A.sort()
BC.sort(reverse=True, key=lambda x: x[1])

count = 0
CArr = []
for b, c in BC:
    if count >= N:
        break
    for i in range(b):
        CArr.append(c)
        count += 1
        if count >= N:
            break

for i in range(len(CArr)):
    if A[i] < CArr[i]:
        A[i] = CArr[i]

ans = sum(A)
print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())

A = list(map(int, input().split()))

B = []
for i in range(M):
    b, c = map(int, input().split())
    for j in range(b):
        B.append(c)

A.sort()
B.sort(reverse=True)
for i in range(M):
    if A[i] < B[i]:
        A[i] = B[i]

ans = sum(A)
print(ans)

Fixed submission:
N, M = map(int, input().split())

A = list(map(int, input().split()))

count = 0
BC = []
for i in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))

A.sort()
BC.sort(reverse=True, key=lambda x: x[1])

count = 0
CArr = []
for b, c in BC:
    if count >= N:
        break
    for i in range(b):
        CArr.append(c)
        count += 1
        if count >= N:
            break

for i in range(len(CArr)):
    if A[i] < CArr[i]:
        A[i] = CArr[i]

ans = sum(A)
print(ans)

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 = map(int, input().split())
    for j in range(b):
        B.append(c)

A.sort()
B.sort(reverse=True)
for i in range(N):
    if i < len(B) and A[i] < B[i]:
        A[i] = B[i]

ans = sum(A)
print(ans)

Fixed submission:
N, M = map(int, input().split())

A = list(map(int, input().split()))

count = 0
BC = []
for i in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))

A.sort()
BC.sort(reverse=True, key=lambda x: x[1])

count = 0
CArr = []
for b, c in BC:
    if count >= N:
        break
    for i in range(b):
        CArr.append(c)
        count += 1
        if count >= N:
            break

for i in range(len(CArr)):
    if A[i] < CArr[i]:
        A[i] = CArr[i]

ans = sum(A)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())

A = list(map(int, input().split()))

for i in range(M):
    A.sort()
    b, c = map(int, input().split())
    for j in range(b):
        if A[j] < c:
            A[j] = c

ans = sum(A)
print(ans)

Fixed submission:
N, M = map(int, input().split())

A = list(map(int, input().split()))

count = 0
BC = []
for i in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))

A.sort()
BC.sort(reverse=True, key=lambda x: x[1])

count = 0
CArr = []
for b, c in BC:
    if count >= N:
        break
    for i in range(b):
        CArr.append(c)
        count += 1
        if count >= N:
            break

for i in range(len(CArr)):
    if A[i] < CArr[i]:
        A[i] = CArr[i]

ans = sum(A)
print(ans)