Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
Q = []

for i in range(M):
    Q.append(tuple(map(int, input().split())))

A.sort()

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

cur = 0
for i in range(M):
    cnt = Q[i][0]
    while A[cur] < Q[i][1] and cnt > 0 and cur < N:
        A[cur] = Q[i][1]
        cur += 1
        cnt -= 1
    
    if A[cur] >= Q[i][1]:
        break

print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
Q = []

for i in range(M):
    Q.append(tuple(map(int, input().split())))

A.sort()

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

cur = 0
for i in range(M):
    cnt = Q[i][0]
    while cnt > 0 and cur < N:
        if A[cur] >= Q[i][1]:
            break
        A[cur] = Q[i][1]
        cur += 1
        cnt -= 1

print(sum(A))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
Q = []

for i in range(M):
    Q.append(tuple(map(int, input().split())))

A.sort()

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

cur = 0
for i in range(M):
    cnt = Q[i][0]
    while A[cur] < Q[i][1] and cnt > 0:
        A[cur] = Q[i][1]
        cur += 1
        cnt -= 1
    
    if A[cur] >= Q[i][1]:
        break

print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
Q = []

for i in range(M):
    Q.append(tuple(map(int, input().split())))

A.sort()

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

cur = 0
for i in range(M):
    cnt = Q[i][0]
    while cnt > 0 and cur < N:
        if A[cur] >= Q[i][1]:
            break
        A[cur] = Q[i][1]
        cur += 1
        cnt -= 1

print(sum(A))

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

N, M = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
heapq.heapify(A)

for i in range(M):
    B, C = map(int, input().split())

    for i in range(B):
        tmp = heapq.heappop(A)
        if tmp < C:
            heapq.heappush(A, C)
        else:
            heapq.heappush(A, tmp)

print(sum(A))



Fixed submission:
N, M = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
Q = []

for i in range(M):
    Q.append(tuple(map(int, input().split())))

A.sort()

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

cur = 0
for i in range(M):
    cnt = Q[i][0]
    while cnt > 0 and cur < N:
        if A[cur] >= Q[i][1]:
            break
        A[cur] = Q[i][1]
        cur += 1
        cnt -= 1

print(sum(A))

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

N, M = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
heapq.heapify(A)

for i in range(M):
    B, C = map(int, input().split())

    for i in range(B):
        tmp = heapq.heappop(A)
        if tmp < C:
            heapq.heappush(A, C)
        else:
            heapq.heappush(A, tmp)

print(sum(A))



Fixed submission:
N, M = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
Q = []

for i in range(M):
    Q.append(tuple(map(int, input().split())))

A.sort()

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

cur = 0
for i in range(M):
    cnt = Q[i][0]
    while cnt > 0 and cur < N:
        if A[cur] >= Q[i][1]:
            break
        A[cur] = Q[i][1]
        cur += 1
        cnt -= 1

print(sum(A))