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

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
for i in range(M):
    B,C = (int(i) for i in input().split())
    D.extend([C]*B)
    len += B
    cnt = min(N,len)
    D.sort(reverse = True)
    D = D[0:cnt]

for i in range(cnt):
    if A[0] < D[i]:
        heapq.heapreplace(A,D[i])
    else:
        break
print(sum(A))


Fixed submission:
import heapq

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
BC = [0] * M

for i in range(M):
    BC[i] = list(map(int,input().split()))
BC.sort(key = lambda x:x[1],reverse = True)
for i in range(M):
    D.extend([BC[i][1]] * BC[i][0])
    len += BC[i][0]
    if len > N:
        break
cnt = min(N,len)
for i in range(cnt):
    if A[0] < D[i]:
        heapq.heapreplace(A,D[i])
    else:
        break
print(sum(A))

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

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
for i in range(M):
    B,C = (int(i) for i in input().split())
    D.extend([C]*B)
    len += B
D.sort()
cnt = min(N,len)
for i in range(cnt):
    if A[0] < D[-1]:
        heapq.heapreplace(A,D[-1])
        D.pop(-1)
    else:
        break
print(sum(A))


Fixed submission:
import heapq

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
BC = [0] * M

for i in range(M):
    BC[i] = list(map(int,input().split()))
BC.sort(key = lambda x:x[1],reverse = True)
for i in range(M):
    D.extend([BC[i][1]] * BC[i][0])
    len += BC[i][0]
    if len > N:
        break
cnt = min(N,len)
for i in range(cnt):
    if A[0] < D[i]:
        heapq.heapreplace(A,D[i])
    else:
        break
print(sum(A))

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

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
for i in range(M):
    B,C = (int(i) for i in input().split())
    D.extend([C]*B)
    len += B
D.sort()
cnt = min(N,len)
for i in range(cnt):
    if A[0] < D[-1]:
        heapq.heapreplace(A,D[-1])
        D.pop(-1)
    else:
        break
print(sum(A))


Fixed submission:
import heapq

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
BC = [0] * M

for i in range(M):
    BC[i] = list(map(int,input().split()))
BC.sort(key = lambda x:x[1],reverse = True)
for i in range(M):
    D.extend([BC[i][1]] * BC[i][0])
    len += BC[i][0]
    if len > N:
        break
cnt = min(N,len)
for i in range(cnt):
    if A[0] < D[i]:
        heapq.heapreplace(A,D[i])
    else:
        break
print(sum(A))

Buggy python submission (verdict: Runtime Error):
import heapq

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
for i in range(M):
    B,C = (int(i) for i in input().split())
    for j in range(B):
        heapq.heappush(D,C)
        len += 1
        if len > N:
            heapq.heappop(D)
            len -= 1

for i in range(N):
    if A[0] < D[-1]:
        heapq.heapreplace(A,D[-1])
        D.pop(-1)
    else:
        break
print(sum(A))


Fixed submission:
import heapq

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
BC = [0] * M

for i in range(M):
    BC[i] = list(map(int,input().split()))
BC.sort(key = lambda x:x[1],reverse = True)
for i in range(M):
    D.extend([BC[i][1]] * BC[i][0])
    len += BC[i][0]
    if len > N:
        break
cnt = min(N,len)
for i in range(cnt):
    if A[0] < D[i]:
        heapq.heapreplace(A,D[i])
    else:
        break
print(sum(A))

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

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()

for i in range(M):
    B,C = (int(i) for i in input().split())
    i = 0
    while C > A[0] and i < B:
        heapq.heappop(A)
        heapq.heappush(A,C)
        i += 1

print(sum(A))


Fixed submission:
import heapq

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
BC = [0] * M

for i in range(M):
    BC[i] = list(map(int,input().split()))
BC.sort(key = lambda x:x[1],reverse = True)
for i in range(M):
    D.extend([BC[i][1]] * BC[i][0])
    len += BC[i][0]
    if len > N:
        break
cnt = min(N,len)
for i in range(cnt):
    if A[0] < D[i]:
        heapq.heapreplace(A,D[i])
    else:
        break
print(sum(A))

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

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()

for i in range(M):
    B,C = (int(i) for i in input().split())
    i = 0
    while C > A[0] and i < B:
        heapq.heappop(A)
        heapq.heappush(A,C)
        i += 1

print(sum(A))


Fixed submission:
import heapq

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
BC = [0] * M

for i in range(M):
    BC[i] = list(map(int,input().split()))
BC.sort(key = lambda x:x[1],reverse = True)
for i in range(M):
    D.extend([BC[i][1]] * BC[i][0])
    len += BC[i][0]
    if len > N:
        break
cnt = min(N,len)
for i in range(cnt):
    if A[0] < D[i]:
        heapq.heapreplace(A,D[i])
    else:
        break
print(sum(A))

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

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()

for i in range(M):
    BC = list(map(int,input().split()))
    i = 0
    while BC[1] > A[0] and i < BC[0]:
        heapq.heappop(A)
        heapq.heappush(A,BC[1])
        i += 1

print(sum(A))


Fixed submission:
import heapq

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
BC = [0] * M

for i in range(M):
    BC[i] = list(map(int,input().split()))
BC.sort(key = lambda x:x[1],reverse = True)
for i in range(M):
    D.extend([BC[i][1]] * BC[i][0])
    len += BC[i][0]
    if len > N:
        break
cnt = min(N,len)
for i in range(cnt):
    if A[0] < D[i]:
        heapq.heapreplace(A,D[i])
    else:
        break
print(sum(A))

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

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
BC = [0] * M
for i in range(M):
    BC = list(map(int,input().split()))
    i = 0
    while i < BC[0] and BC[1] > A[0]:
        heapq.heappop(A)
        heapq.heappush(A,BC[1])
        i += 1

print(sum(A))


Fixed submission:
import heapq

N,M = (int(i) for i in input().split())
A = list(map(int,input().split()))
A.sort()
D = []
len = 0
BC = [0] * M

for i in range(M):
    BC[i] = list(map(int,input().split()))
BC.sort(key = lambda x:x[1],reverse = True)
for i in range(M):
    D.extend([BC[i][1]] * BC[i][0])
    len += BC[i][0]
    if len > N:
        break
cnt = min(N,len)
for i in range(cnt):
    if A[0] < D[i]:
        heapq.heapreplace(A,D[i])
    else:
        break
print(sum(A))