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())

    for j in range(B):
        a = heapq.heappop(A)
        heapq.heappush(A, max(C, a))
print(sum(A))

Fixed submission:
import  heapq

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

heapq.heapify(A)
B_C_list = []
for i in range(M):
    B, C = map(int, input().split())
    B_C_list.append((B, C))

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

for B_C in B_C_list:
    a = heapq.heappop(A)
    if a >= B_C[1]:
        heapq.heappush(A, a)
        break
    else:
        heapq.heappush(A, a)
        for j in range(B_C[0]):
            a = heapq.heappop(A)
            if a >= B_C[1]:
                heapq.heappush(A, a)
                break
            heapq.heappush(A, B_C[1])
print(sum(A))

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

N, M = map(int, input().split())
A = list(map(int, input().split()))
A_neg = [-a for a in A]
for i in range(M):
    b, c = map(int, input().split())
    c_list = [-c] * b
    A_neg.extend(c_list)
heapq.heapify(A_neg)
count = 0
for i in range(N):
    count += -heapq.heappop(A_neg)
print(count)

Fixed submission:
import  heapq

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

heapq.heapify(A)
B_C_list = []
for i in range(M):
    B, C = map(int, input().split())
    B_C_list.append((B, C))

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

for B_C in B_C_list:
    a = heapq.heappop(A)
    if a >= B_C[1]:
        heapq.heappush(A, a)
        break
    else:
        heapq.heappush(A, a)
        for j in range(B_C[0]):
            a = heapq.heappop(A)
            if a >= B_C[1]:
                heapq.heappush(A, a)
                break
            heapq.heappush(A, B_C[1])
print(sum(A))

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

N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in range(M):
    b, c = map(int, input().split())
    c_list = [c] * b
    A.extend(c_list)
A.sort(reverse=True)

print(sum(A[:N]))

Fixed submission:
import  heapq

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

heapq.heapify(A)
B_C_list = []
for i in range(M):
    B, C = map(int, input().split())
    B_C_list.append((B, C))

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

for B_C in B_C_list:
    a = heapq.heappop(A)
    if a >= B_C[1]:
        heapq.heappush(A, a)
        break
    else:
        heapq.heappush(A, a)
        for j in range(B_C[0]):
            a = heapq.heappop(A)
            if a >= B_C[1]:
                heapq.heappush(A, a)
                break
            heapq.heappush(A, B_C[1])
print(sum(A))

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())
    for j in range(b):
        q = heapq.heappop(A)
        if q > c:
            heapq.heappush(A, q)
            break
        else:
            heapq.heappush(A, c)
print(sum(A))

Fixed submission:
import  heapq

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

heapq.heapify(A)
B_C_list = []
for i in range(M):
    B, C = map(int, input().split())
    B_C_list.append((B, C))

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

for B_C in B_C_list:
    a = heapq.heappop(A)
    if a >= B_C[1]:
        heapq.heappush(A, a)
        break
    else:
        heapq.heappush(A, a)
        for j in range(B_C[0]):
            a = heapq.heappop(A)
            if a >= B_C[1]:
                heapq.heappush(A, a)
                break
            heapq.heappush(A, B_C[1])
print(sum(A))

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())
    for j in range(b):
        q = heapq.heappop(A)
        if q < c:
            heapq.heappush(A, c)
        else:
            heapq.heappush(A, q)
            break
print(sum(A))

Fixed submission:
import  heapq

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

heapq.heapify(A)
B_C_list = []
for i in range(M):
    B, C = map(int, input().split())
    B_C_list.append((B, C))

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

for B_C in B_C_list:
    a = heapq.heappop(A)
    if a >= B_C[1]:
        heapq.heappush(A, a)
        break
    else:
        heapq.heappush(A, a)
        for j in range(B_C[0]):
            a = heapq.heappop(A)
            if a >= B_C[1]:
                heapq.heappush(A, a)
                break
            heapq.heappush(A, B_C[1])
print(sum(A))