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 j in range(b):
    d = heapq.heappushpop(A, c)
    if c == d:
      break
      
print(sum(A))

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

for i in range(M):
  exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(M):
  e += [exc[i][1]] * exc[i][0]
  count += exc[i][0]
  if count >= N:
    break
cards = sorted(A+e, reverse=True)
print(sum(cards[:N]))

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 j in range(b):
    d = heapq.heappushpop(A, c)
    if c == d:
      break
      
print(sum(A))

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

for i in range(M):
  exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(M):
  e += [exc[i][1]] * exc[i][0]
  count += exc[i][0]
  if count >= N:
    break
cards = sorted(A+e, reverse=True)
print(sum(cards[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
ans = 0
for i in range(M):
  b, c = map(int, input().split())
  if c <= min(A):
    continue
  b = min(b, N)
  A += [c for j in range(b)]
  heapq.heapify(A)
  for i in range(b):
    heapq.heappop(A)
  
print(sum(A))

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

for i in range(M):
  exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(M):
  e += [exc[i][1]] * exc[i][0]
  count += exc[i][0]
  if count >= N:
    break
cards = sorted(A+e, reverse=True)
print(sum(cards[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
ans = 0
for i in range(M):
  b, c = map(int, input().split())
  if c <= min(A):
    continue
  b = min(b, N)
  A += [c for j in range(b)]
  heapq.heapify(A)
  for i in range(b):
    heapq.heappop(A)
  
print(sum(A))

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

for i in range(M):
  exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(M):
  e += [exc[i][1]] * exc[i][0]
  count += exc[i][0]
  if count >= N:
    break
cards = sorted(A+e, reverse=True)
print(sum(cards[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
A = list(map(lambda x: int(x), input().split()))
ans = 0
for i in range(M):
  b, c = map(int, input().split())
  b = min(b, N)
  A += [c for j in range(b)]
  heapq.heapify(A)
  for i in range(b):
    heapq.heappop(A)
  
print(sum(A))

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

for i in range(M):
  exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(M):
  e += [exc[i][1]] * exc[i][0]
  count += exc[i][0]
  if count >= N:
    break
cards = sorted(A+e, reverse=True)
print(sum(cards[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
A = list(map(lambda x: -int(x), input().split()))
ans = 0
for i in range(M):
  b, c = map(int, input().split())
  b = min(b, N)
  A += [-c for j in range(b)]
heapq.heapify(A)
for i in range(N):
  ans += -heapq.heappop(A)
print(ans)

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

for i in range(M):
  exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(M):
  e += [exc[i][1]] * exc[i][0]
  count += exc[i][0]
  if count >= N:
    break
cards = sorted(A+e, reverse=True)
print(sum(cards[:N]))

Buggy python submission (verdict: Runtime Error):
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
s = 0
for i in range(M):
  b, c = map(int, input().split())
  b = min(b, N)
  A += heapq.heapify([c for j in range(b)])
  s += b
  heapq.heapify(A)
  for i in range(b):
    A_min = heapq.heappop(A)
    if A_min == c:
      break
print(sum(A))

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

for i in range(M):
  exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(M):
  e += [exc[i][1]] * exc[i][0]
  count += exc[i][0]
  if count >= N:
    break
cards = sorted(A+e, reverse=True)
print(sum(cards[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
s = 0
for i in range(M):
  b, c = map(int, input().split())
  b = min(b, N)
  A += [c for j in range(b)]
  s += b
heapq.heapify(A)
for i in range(s):
  heapq.heappop(A)
print(sum(A))

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

for i in range(M):
  exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(M):
  e += [exc[i][1]] * exc[i][0]
  count += exc[i][0]
  if count >= N:
    break
cards = sorted(A+e, reverse=True)
print(sum(cards[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
s = 0
for i in range(M):
  b, c = map(int, input().split())
  A += [c for j in range(b)]
  s += b
heapq.heapify(A)
for i in range(s):
  heapq.heappop(A)
print(sum(A))

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

for i in range(M):
  exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(M):
  e += [exc[i][1]] * exc[i][0]
  count += exc[i][0]
  if count >= N:
    break
cards = sorted(A+e, reverse=True)
print(sum(cards[:N]))