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):
    c = heapq.heappushpop(A, C)
    if c == C:
      break

print(sum(A))


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

CB = []
for i in range(M):
  b, c = map(int, input().split())
  CB.append([c, b])

D = [[a, 1] for a in A] + CB

D.sort()

ans = 0
while N > 0:
  c, b = D.pop()
  ans += min(N, b)*c
  N -= b

print(ans)

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

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

CB = []
for i in range(M):
  b, c = map(int, input().split())
  CB.append([c, b])

CB.sort()
D = []
for c, b in CB:
  D += [c]*b

for i in range(N):
  if D:
    d = D.pop()
    g = heapq.heappushpop(A, d)
    if d == g:
      break
  else:
    break

print(sum(A))

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

CB = []
for i in range(M):
  b, c = map(int, input().split())
  CB.append([c, b])

D = [[a, 1] for a in A] + CB

D.sort()

ans = 0
while N > 0:
  c, b = D.pop()
  ans += min(N, b)*c
  N -= b

print(ans)

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

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

CB = []
for i in range(M):
  b, c = map(int, input().split())
  CB.append([c, b])

CB.sort()
D = []
for c, b in CB:
  D += [c]*b

for i in range(N):
  d = D.pop()
  g = heapq.heappushpop(A, d)
  if d == g:
    break

print(sum(A))

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

CB = []
for i in range(M):
  b, c = map(int, input().split())
  CB.append([c, b])

D = [[a, 1] for a in A] + CB

D.sort()

ans = 0
while N > 0:
  c, b = D.pop()
  ans += min(N, b)*c
  N -= b

print(ans)

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

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

for i in range(M):
  B, C = map(int, input().split())
  idx = bisect.bisect_left(A, C)
  if B < idx:
    A = A[B:idx] + [C]*B + A[idx:]
  else:
    A = [C]*idx + A[idx:]
  
print(sum(A))

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

CB = []
for i in range(M):
  b, c = map(int, input().split())
  CB.append([c, b])

D = [[a, 1] for a in A] + CB

D.sort()

ans = 0
while N > 0:
  c, b = D.pop()
  ans += min(N, b)*c
  N -= b

print(ans)

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):
    c = heapq.heappushpop(A, C)
    if c == C:
      break

print(sum(A))

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

CB = []
for i in range(M):
  b, c = map(int, input().split())
  CB.append([c, b])

D = [[a, 1] for a in A] + CB

D.sort()

ans = 0
while N > 0:
  c, b = D.pop()
  ans += min(N, b)*c
  N -= b

print(ans)