Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

changes = defaultdict(int)
for i in range(M):
    b, c = map(int, input().split())
    changes[c] += b

changes = sorted(changes.items(), key = lambda x:x[0], reverse=True)

d = []
len_d = 0
for v in changes:
    d += [v[0]]*v[1]
    len_d += v[1]
    if len_d >= N:
        break

res = 0
i = 0
while i < min(len(d), N):
    if A[i] < d[i]:
        res += d[i]
    else:
        res += A[i]
    i += 1

if i < N:
    for j in range(i, N):
        res += A[j]

print(res)


Fixed submission:
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [(A[i], 1) for i in range(N)]

changes = [None] * M
for i in range(M):
    b, c = map(int, input().split())
    changes[i] = (c, b)

allVals = A + changes

allVals.sort(key = lambda x:x[0], reverse = True)
# print(allVals)

i = 0
j = -1
res = 0
while True:
    j += 1
    res += allVals[j][0] * allVals[j][1]
    i += allVals[j][1]

    if i >= N:
        res -= allVals[j][0] * (i - N)
        break



print(res)

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

changes = defaultdict(int)
for i in range(M):
    b, c = map(int, input().split())
    changes[c] += b

changes = sorted(changes.items(), key = lambda x:x[0], reverse=True)

d = []
len_d = 0
for v in changes:
    d += [v[0]]*v[1]
    len_d += v[1]
    if len_d >= N:
        d = d[0:N]
        break

res = 0
i = 0
while i < min(len(d), N):
    if A[i] <= d[i]:
        res += d[i]
    else:
        res += A[i]
    i += 1

if i < N:
    for j in range(i, N):
        res += A[j]

print(res)


Fixed submission:
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [(A[i], 1) for i in range(N)]

changes = [None] * M
for i in range(M):
    b, c = map(int, input().split())
    changes[i] = (c, b)

allVals = A + changes

allVals.sort(key = lambda x:x[0], reverse = True)
# print(allVals)

i = 0
j = -1
res = 0
while True:
    j += 1
    res += allVals[j][0] * allVals[j][1]
    i += allVals[j][1]

    if i >= N:
        res -= allVals[j][0] * (i - N)
        break



print(res)

Buggy python submission (verdict: Runtime Error):
import heapq
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

changes = defaultdict(int)
for i in range(M):
    b, c = map(int, input().split())
    changes[c] += b

changes = sorted(changes.items(), key = lambda x:x[0], reverse=True)

d = []
len_d = 0
for v in changes:
    d += [v[0]]*v[1]
    len_d += v[1]
    if len_d > N:
        d = d[0:N]
        break

res = 0
i = 0
while i < len(d):
    if A[i] <= d[i]:
        res += d[i]
    else:
        res += A[i]
    i += 1

if i < N:
    for j in range(i, N):
        res += A[j]

print(res)


Fixed submission:
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [(A[i], 1) for i in range(N)]

changes = [None] * M
for i in range(M):
    b, c = map(int, input().split())
    changes[i] = (c, b)

allVals = A + changes

allVals.sort(key = lambda x:x[0], reverse = True)
# print(allVals)

i = 0
j = -1
res = 0
while True:
    j += 1
    res += allVals[j][0] * allVals[j][1]
    i += allVals[j][1]

    if i >= N:
        res -= allVals[j][0] * (i - N)
        break



print(res)

Buggy python submission (verdict: Runtime Error):
import heapq
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

changes = defaultdict(int)
for i in range(M):
    b, c = map(int, input().split())
    changes[c] += b

changes = sorted(changes.items(), key = lambda x:x[0], reverse=True)

d = []
len_d = 0
for v in changes:
    d += [v[0]]*v[1]
    len_d += v[1]
    if len_d > N:
        d = d[0:N]
        break


res = 0
i = 0
while i < min(len(d), len(A)):
    if A[i] <= d[i]:
        res += d[i]
    else:
        res += A[i]
    i += 1

if i < N:
    for j in range(i, N):
        res += A[j]

print(res)


Fixed submission:
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [(A[i], 1) for i in range(N)]

changes = [None] * M
for i in range(M):
    b, c = map(int, input().split())
    changes[i] = (c, b)

allVals = A + changes

allVals.sort(key = lambda x:x[0], reverse = True)
# print(allVals)

i = 0
j = -1
res = 0
while True:
    j += 1
    res += allVals[j][0] * allVals[j][1]
    i += allVals[j][1]

    if i >= N:
        res -= allVals[j][0] * (i - N)
        break



print(res)

Buggy python submission (verdict: Runtime Error):
import heapq
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

changes = defaultdict(int)
for i in range(M):
    b, c = map(int, input().split())
    changes[c] += b

changes = sorted(changes.items(), key = lambda x:x[0], reverse=True)

d = []
len_d = 0
for v in changes:
    d += [v[0]]*v[1]
    len_d += v[1]
    if len_d > N:
        d = d[0:N]
        break


res = 0
i = 0
while i < min(len(d), len(A)):
    if A[i] < d[i]:
        res += d[i]
    else:
        res += A[i]
    i += 1

if i < len(A):
    for j in range(i, N):
        res += A[j]

print(res)


Fixed submission:
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [(A[i], 1) for i in range(N)]

changes = [None] * M
for i in range(M):
    b, c = map(int, input().split())
    changes[i] = (c, b)

allVals = A + changes

allVals.sort(key = lambda x:x[0], reverse = True)
# print(allVals)

i = 0
j = -1
res = 0
while True:
    j += 1
    res += allVals[j][0] * allVals[j][1]
    i += allVals[j][1]

    if i >= N:
        res -= allVals[j][0] * (i - N)
        break



print(res)

Buggy python submission (verdict: Runtime Error):
import heapq
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

changes = defaultdict(int)
for i in range(M):
    b, c = map(int, input().split())
    changes[c] += b

changes = sorted(changes.items(), key = lambda x:x[0], reverse=True)

d = []
len_d = 0
for v in changes:
    d += [v[0]]*v[1]
    len_d += v[1]
    if len_d > N:
        d = d[0:N]
        break


res = 0
i = 0
while i < min(len(d), len(A)):
    if A[i] < d[i]:
        res += d[i]
    else:
        res += A[i]
    i += 1

if i < len(A):
    for j in range(i, N):
        res += A[j]

print(res)


Fixed submission:
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [(A[i], 1) for i in range(N)]

changes = [None] * M
for i in range(M):
    b, c = map(int, input().split())
    changes[i] = (c, b)

allVals = A + changes

allVals.sort(key = lambda x:x[0], reverse = True)
# print(allVals)

i = 0
j = -1
res = 0
while True:
    j += 1
    res += allVals[j][0] * allVals[j][1]
    i += allVals[j][1]

    if i >= N:
        res -= allVals[j][0] * (i - N)
        break



print(res)

Buggy python submission (verdict: Runtime Error):
import heapq
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

changes = defaultdict(int)
for i in range(M):
    b, c = map(int, input().split())
    changes[c] += b

changes = sorted(changes.items(), key = lambda x:x[0], reverse=True)

d = []
len_d = 0
for v in changes:
    d += [v[0]]*v[1]
    len_d += v[1]
    if len_d >= N:
        d = d[0:N]
        break


res = 0
i = 0
while i < min(len(d), len(A)):
    if A[i] < d[i]:
        res += d[i]
    else:
        res += A[i]
    i += 1

if i < len(A):
    for j in range(i, N):
        res += A[j]

print(res)


Fixed submission:
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [(A[i], 1) for i in range(N)]

changes = [None] * M
for i in range(M):
    b, c = map(int, input().split())
    changes[i] = (c, b)

allVals = A + changes

allVals.sort(key = lambda x:x[0], reverse = True)
# print(allVals)

i = 0
j = -1
res = 0
while True:
    j += 1
    res += allVals[j][0] * allVals[j][1]
    i += allVals[j][1]

    if i >= N:
        res -= allVals[j][0] * (i - N)
        break



print(res)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)

for i in range(M):
    b, c = map(int, input().split())

    count = 1
    while count <= b:
        top = A[0]
        if top <= c:
            heapq.heappop(A)
            heapq.heappush(A, c)
            count += 1
        else:
            break

ans = sum(A)
print(ans)


Fixed submission:
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [(A[i], 1) for i in range(N)]

changes = [None] * M
for i in range(M):
    b, c = map(int, input().split())
    changes[i] = (c, b)

allVals = A + changes

allVals.sort(key = lambda x:x[0], reverse = True)
# print(allVals)

i = 0
j = -1
res = 0
while True:
    j += 1
    res += allVals[j][0] * allVals[j][1]
    i += allVals[j][1]

    if i >= N:
        res -= allVals[j][0] * (i - N)
        break



print(res)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

heapq.heapify(A)

for i in range(M):
    b, c = map(int, input().split())

    count = 1
    while count <= b:
        top = A[0]
        if top <= c:
            heapq.heappop(A)
            heapq.heappush(A, c)
            count += 1
        else:
            break

ans = sum(A)
print(ans)


Fixed submission:
from collections import defaultdict
N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [(A[i], 1) for i in range(N)]

changes = [None] * M
for i in range(M):
    b, c = map(int, input().split())
    changes[i] = (c, b)

allVals = A + changes

allVals.sort(key = lambda x:x[0], reverse = True)
# print(allVals)

i = 0
j = -1
res = 0
while True:
    j += 1
    res += allVals[j][0] * allVals[j][1]
    i += allVals[j][1]

    if i >= N:
        res -= allVals[j][0] * (i - N)
        break



print(res)