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

N, M = map(int, input().split())
A = [int(a) * (-1) for a in input().split()]

for _ in range(M):
    B, C = map(int, input().split())
    tmp = [C * (-1)] * B
    A.extend(tmp)

heapq.heapify(A)

ans = 0
for _ in range(N):
    ans += heapq.heappop(A)

print(ans * (-1))

Fixed submission:
import heapq

N, M = map(int, input().split())
A = [int(a) * (-1) for a in input().split()]

B_array = []
for _ in range(M):
    B, C = map(int, input().split())
    B_array.append([C * (-1), B])
B_array.sort()

cnt = 0
i = 0
B_list = []
while cnt < N and i < M:
    B_list.extend([B_array[i][0]] * B_array[i][1])
    cnt += B_array[i][1]
    i += 1

A.extend(B_list)
heapq.heapify(A)

ans = 0
for _ in range(N):
    ans += heapq.heappop(A)
        
print(ans * (-1))

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

N, M = map(int, input().split())
A = [int(a) for a in input().split()]
comp = []
for _ in range(M):
    B, C = map(int, input().split())
    tmp = [C] * B
    comp.extend(tmp)

A.sort()
comp.sort(reverse=True)

zeros = [0] * N
comp.extend(zeros)
comp = comp[:N]

for i in range(N):
    if A[i] < comp[i]:
        A[i] = comp[i]
            
print(sum(A))

Fixed submission:
import heapq

N, M = map(int, input().split())
A = [int(a) * (-1) for a in input().split()]

B_array = []
for _ in range(M):
    B, C = map(int, input().split())
    B_array.append([C * (-1), B])
B_array.sort()

cnt = 0
i = 0
B_list = []
while cnt < N and i < M:
    B_list.extend([B_array[i][0]] * B_array[i][1])
    cnt += B_array[i][1]
    i += 1

A.extend(B_list)
heapq.heapify(A)

ans = 0
for _ in range(N):
    ans += heapq.heappop(A)
        
print(ans * (-1))

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

N, M = map(int, input().split())
A = [int(a) for a in input().split()]
comp = []
for _ in range(M):
    B, C = map(int, input().split())
    for i in range(B):
        comp.append(C)

A.sort()
comp.sort(reverse=True)

zeros = [0] * N
comp.extend(zeros)
comp = comp[:N]

for i in range(N):
    if A[i] < comp[i]:
        A[i] = comp[i]

print(sum(A))

Fixed submission:
import heapq

N, M = map(int, input().split())
A = [int(a) * (-1) for a in input().split()]

B_array = []
for _ in range(M):
    B, C = map(int, input().split())
    B_array.append([C * (-1), B])
B_array.sort()

cnt = 0
i = 0
B_list = []
while cnt < N and i < M:
    B_list.extend([B_array[i][0]] * B_array[i][1])
    cnt += B_array[i][1]
    i += 1

A.extend(B_list)
heapq.heapify(A)

ans = 0
for _ in range(N):
    ans += heapq.heappop(A)
        
print(ans * (-1))

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

N, M = map(int, input().split())
A = [int(a) for a in input().split()]
B = []
for _ in range(M):
    tmp = [int(b) for b in input().split()]
    B.append(tmp)

heapq.heapify(A)

for i in range(M):
    for j in range(B[i][0]):
        val = heapq.heappop(A)
        if val < B[i][1]:
            heapq.heappush(A, B[i][1])
        else:
            heapq.heappush(A, val)
            
print(sum(A))

Fixed submission:
import heapq

N, M = map(int, input().split())
A = [int(a) * (-1) for a in input().split()]

B_array = []
for _ in range(M):
    B, C = map(int, input().split())
    B_array.append([C * (-1), B])
B_array.sort()

cnt = 0
i = 0
B_list = []
while cnt < N and i < M:
    B_list.extend([B_array[i][0]] * B_array[i][1])
    cnt += B_array[i][1]
    i += 1

A.extend(B_list)
heapq.heapify(A)

ans = 0
for _ in range(N):
    ans += heapq.heappop(A)
        
print(ans * (-1))