Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
Z = []
ope = []
for _ in range(M):
  ope.append(list(map(int, input().split())))
ope.sort(key=lambda x:x[1], reverse=True)
pk = np.array(ope)
totalB = sum(pk[:, 0])

while int(len(Z)) < min(N, totalB):
  for o in ope:
    for _ in range(o[0]):
      Z.append(o[1])

for i in range(min(N, len(Z))):
  if Z[i] > A[i]:
      A[i] = Z[i]
  else:
    break
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
pool = []
[pool.append((a, 1)) for a in A]
for _ in range(M):
  b, c = (map(int, input().split()))
  pool.append((c, b))
pool.sort(reverse=True, key=lambda x:x[0])

while N>0:
  for p in pool:
    if p[1] >= N:
      ans += p[0]*N
      N = 0
    if p[1] < N:
      ans += p[0]*p[1]
      N -= p[1]

print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
Z = []
for _ in range(M):
  B, C = map(int, input().split())
  [Z.append(C) for _ in range(B)]
Z.sort(reverse=True)
for i in range(N):
  if Z[i] > A[i]:
      A[i] = Z[i]
  else:
    break
print(sum(A))  

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
pool = []
[pool.append((a, 1)) for a in A]
for _ in range(M):
  b, c = (map(int, input().split()))
  pool.append((c, b))
pool.sort(reverse=True, key=lambda x:x[0])

while N>0:
  for p in pool:
    if p[1] >= N:
      ans += p[0]*N
      N = 0
    if p[1] < N:
      ans += p[0]*p[1]
      N -= p[1]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
ope = []
for _ in range(M):
  ope.append(list(map(int, input().split())))
for m in ope:
  for i in range(m[0]):
    if m[1] > A[i]:
      A[i] = m[1]
  A.sort()
print(sum(A))  

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
pool = []
[pool.append((a, 1)) for a in A]
for _ in range(M):
  b, c = (map(int, input().split()))
  pool.append((c, b))
pool.sort(reverse=True, key=lambda x:x[0])

while N>0:
  for p in pool:
    if p[1] >= N:
      ans += p[0]*N
      N = 0
    if p[1] < N:
      ans += p[0]*p[1]
      N -= p[1]

print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
ope = []
for _ in range(M):
  ope.append(list(map(int, input().split())))
for m in ope:
  for i in range(m[0]):
    if m[1] > A[i]:
      A[i] = m[1]
	A.sort()
print(sum(A))      

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
pool = []
[pool.append((a, 1)) for a in A]
for _ in range(M):
  b, c = (map(int, input().split()))
  pool.append((c, b))
pool.sort(reverse=True, key=lambda x:x[0])

while N>0:
  for p in pool:
    if p[1] >= N:
      ans += p[0]*N
      N = 0
    if p[1] < N:
      ans += p[0]*p[1]
      N -= p[1]

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
ope = []
for _ in range(M):
  ope.append(list(map(int, input().split())))
for m in ope:
  for _ in range(m[0]):
    if m[1] > A[0]:
      A[0] = m[1]
      A.sort()
    else:
      break
print(sum(A))      


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
pool = []
[pool.append((a, 1)) for a in A]
for _ in range(M):
  b, c = (map(int, input().split()))
  pool.append((c, b))
pool.sort(reverse=True, key=lambda x:x[0])

while N>0:
  for p in pool:
    if p[1] >= N:
      ans += p[0]*N
      N = 0
    if p[1] < N:
      ans += p[0]*p[1]
      N -= p[1]

print(ans)