Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
L = [tuple(map(int, input().split())) for _ in range(M)]

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

flag = True
num = 0
now = 0
while flag:
    if num >= M:
        break
    for i in range(L[num][0]):
        if now <= N and L[num][1] > A[now]:
            A[now] = L[num][1]
            now += 1
        else:
            flag = False
            break
    num += 1
print(sum(A))



Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
L = [tuple(map(int, input().split())) for _ in range(M)]

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

flag = True
num = 0
now = 0
while flag:
    if num >= M:
        break
    for i in range(L[num][0]):
        if now < N and L[num][1] > A[now]:
            A[now] = L[num][1]
            now += 1
        else:
            flag = False
            break
    num += 1
print(sum(A))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
L = [tuple(map(int, input().split())) for _ in range(M)]

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

flag = True
num = 0
now = 0
while flag:
    if num >= M:
        break
    for i in range(L[num][0]):
        if L[num][1] > A[now]:
            A[now] = L[num][1]
            now += 1
        else:
            flag = False
            break
    num += 1
print(sum(A))



Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
L = [tuple(map(int, input().split())) for _ in range(M)]

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

flag = True
num = 0
now = 0
while flag:
    if num >= M:
        break
    for i in range(L[num][0]):
        if now < N and L[num][1] > A[now]:
            A[now] = L[num][1]
            now += 1
        else:
            flag = False
            break
    num += 1
print(sum(A))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
L = [tuple(map(int, input().split())) for _ in range(M)]

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

flag = True
num = 0
now = 0
while num < M:
    for i in range(L[num][0]):
        if L[num][1] > A[now]:
            A[now] = L[num][1]
            now += 1
        else:
            flag = False
            break
    num += 1
print(sum(A))



Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
L = [tuple(map(int, input().split())) for _ in range(M)]

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

flag = True
num = 0
now = 0
while flag:
    if num >= M:
        break
    for i in range(L[num][0]):
        if now < N and L[num][1] > A[now]:
            A[now] = L[num][1]
            now += 1
        else:
            flag = False
            break
    num += 1
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
L = [tuple(map(int, input().split())) for _ in range(M)]

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

flag = True
num = 0
while flag:
    if num >= M:
        break

    for i in range(L[num][0]):
        if L[num][1] > A[0]:
            A[0] = L[num][1]
            A.sort()
        else:
            flag = False
            break
    num += 1

print(sum(A))



Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
L = [tuple(map(int, input().split())) for _ in range(M)]

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

flag = True
num = 0
now = 0
while flag:
    if num >= M:
        break
    for i in range(L[num][0]):
        if now < N and L[num][1] > A[now]:
            A[now] = L[num][1]
            now += 1
        else:
            flag = False
            break
    num += 1
print(sum(A))