Buggy python submission (verdict: Runtime Error):
from functools import cmp_to_key
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: x[1] - y[1]))
t = 0
for bc in range(m):
  b, c = bc
  t += b
  a.extend([c] * b)
  if t > n:
    break
a.sort(reverse = True)
print(sum(a[:n]))


Fixed submission:
from functools import cmp_to_key
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: y[1] - x[1]))
t = 0
for v in bc:
  b, c = v
  t += b
  a.extend([c] * b)
  if t > n:
    break
a.sort(reverse = True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
for _ in range(m):
  b, c = map(int, input().split())
  a.extend([c] * b)
a.sort(reverse = True)
print(sum(a[:n]))


Fixed submission:
from functools import cmp_to_key
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: y[1] - x[1]))
t = 0
for v in bc:
  b, c = v
  t += b
  a.extend([c] * b)
  if t > n:
    break
a.sort(reverse = True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
from functools import cmp_to_key
from heapq import heappush, heappop, heapify, heapreplace
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
heapify(a)
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: x[1] - y[1]))
for i in range(m):
  b, c = bc[i]
  for j in range(b):
    if a[0] >= c:
      break
    else:
      heapreplace(a, c)
print(sum(a))


Fixed submission:
from functools import cmp_to_key
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: y[1] - x[1]))
t = 0
for v in bc:
  b, c = v
  t += b
  a.extend([c] * b)
  if t > n:
    break
a.sort(reverse = True)
print(sum(a[:n]))

Buggy python submission (verdict: Runtime Error):
from functools import cmp_to_key
from heapq import heappush, heappop, heapify, heapreplace
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
heapify(a)
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: x[1] - y[1]))
for i in range(m):
  b, c = bc[i]
  for j in range(b):
    if a[0] >= c:
      break
    else:
      heapreplace(a, c)
print(result)


Fixed submission:
from functools import cmp_to_key
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: y[1] - x[1]))
t = 0
for v in bc:
  b, c = v
  t += b
  a.extend([c] * b)
  if t > n:
    break
a.sort(reverse = True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
from functools import cmp_to_key
from heapq import heappush, heappop, heapify
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
heapify(a)
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: x[1] - y[1]))
for i in range(m):
  b, c = bc[i]
  for j in range(b):
    t = heappop(a)
    if t < c:
      heappush(a, c)
    else:
      heappush(a, t)
      break
print(sum(a))


Fixed submission:
from functools import cmp_to_key
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: y[1] - x[1]))
t = 0
for v in bc:
  b, c = v
  t += b
  a.extend([c] * b)
  if t > n:
    break
a.sort(reverse = True)
print(sum(a[:n]))

Buggy python submission (verdict: Runtime Error):
from functools import cmp_to_key
from heapq import heappush, heappop, heapify
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
heapify(a)
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: x[1] - y[1]))
for i in range(m):
  b, c = bc[i]
  for j in range(b):
    t = heappop(a)
    if t < c:
      heappush(a, c)
    else:
      heappop(a, t)
      break
print(sum(a))


Fixed submission:
from functools import cmp_to_key
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: y[1] - x[1]))
t = 0
for v in bc:
  b, c = v
  t += b
  a.extend([c] * b)
  if t > n:
    break
a.sort(reverse = True)
print(sum(a[:n]))