Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
  if num >= N: break
print(ans+sum(A[num:]) if num != N else ans)

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]))

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

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

input = sys.stdin.readline

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())), reverse=True)
C = []
for _ in range(M):
  b, c = map(int,input().split())
  C.extend([c]*b)
C.sort(reverse=True)
lenC = len(C)
ans = -float('inf')
for i in range(N+1):
  if i == 0:
    if lenC >= N:
      tmp_ans = sum(C[:N])
      idxC = N-1
      idxA = 0
    else:
      tmp_ans = sum(C) + sum(A[:N-lenC])
      idxC = lenC - 1
      idxA = N-lenC
  else:
    if C[idxC] > A[idxA]: break
    tmp_ans = tmp_ans - C[idxC] + A[idxA]
    idxC -= 1
    idxA += 1
  ans = max(ans,tmp_ans)
  if idxC < 0 or idxA > N: break

print(ans)

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

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

input = sys.stdin.readline

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())), reverse=True)
C = []
for _ in range(M):
  b, c = map(int,input().split())
  C.extend([c]*b)
C.sort(reverse=True)
lenC = len(C)
ans = -float('inf')
for i in range(N+1):
  if i == 0:
    if lenC >= N:
      tmp_ans = sum(C[:N])
      idxC = N-1
      idxA = 0
    else:
      tmp_ans = sum(C) + sum(A[:N-lenC])
      idxC = lenC - 1
      idxA = N-lenC
  else:
    if C[idxC] > A[idxA]: break
    tmp_ans = tmp_ans - C[idxC] + A[idxA]
    idxC -= 1
    idxA += 1
  ans = max(ans,tmp_ans)
  if idxC < 0 or idxA > N: break

print(ans)

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

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

input = sys.stdin.readline

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())), reverse=True)
C = []
for _ in range(M):
  b, c = map(int,input().split())
  C.extend([c]*b)
C.sort(reverse=True)
lenC = len(C)
ans = -float('inf')
for i in range(N+1):
  if i == 0:
    if lenC >= N:
      tmp_ans = sum(C[:N])
      idxC = N-1
      idxA = 0
    else:
      tmp_ans = sum(C) + sum(A[:N-lenC])
      idxC = lenC - 1
      idxA = N-lenC
  else:
    #print(lenC, N, idxC, idxA)
    tmp_ans = tmp_ans - C[idxC] + A[idxA]
    idxC -= 1
    idxA += 1
  ans = max(ans,tmp_ans)
  if idxC < 0 or idxA > N: break

print(ans)

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
import sys

input = sys.stdin.readline

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())), reverse=True)
C = []
for _ in range(M):
  b, c = map(int,input().split())
  C.extend([c]*b)
C.sort(reverse=True)

ans = -float('inf')
for i in range(N+1):
  if i == 0: tmp_ans = sum(C[:N])
  elif N-i == 0: tmp_ans = sum(A)
  else:
    tmp_ans = sum(A[:i]) + sum(C[:N-i])
  ans = max(ans,tmp_ans)

print(ans)

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

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

n, m = map(int, input().split())
alist = np.array(list(map(int, input().split())))

for _ in range(m):
  b, c = map(int, input().split())
  alist =np.concatenate([alist, np.array([c for _ in range(b)])], axis=1)

print(np.sum(np.sort(alist)[-n:]))

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

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

n, m = map(int, input().split())
alist = list(map(int, input().split()))

for _ in range(m):
  b, c = map(int, input().split())
  alist.extend([c for _ in range(b)])

print(sum(sorted(alist, reverse=True)[:n]))

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

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

n, m = map(int, input().split())

alist = list(map(int, input().split()))

for _ in range(m):
  b, c = map(int, input().split())
  alist.extend([c for _ in range(b)])

print(sum(sorted(alist, reverse=True)[:n]))

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

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

n, m = map(int, input().split())

alist = sorted(list(map(int, input().split())))

for _ in range(m):
  b, c = map(int, input().split())
  idx = bisect.bisect_right(alist,c)
  if idx-1 <= b:
    alist = [c for _ in range(idx)] + alist[idx:]
  else:
    idx2 = idx-b
    alist = alist[b:idx2+b] + [c for _ in range(b)] + alist[b+idx2:]  
print(sum(alist))

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

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

n, m = map(int, input().split())

alist = sorted(list(map(int, input().split())))

for _ in range(m):
  b, c = map(int, input().split())
  idx = bisect.bisect_right(alist,c)
  if idx-1 <= b:
    alist = [c for _ in range(idx)] + alist[idx:]
  else:
    new_alist = alist[b:]
    idx2 = idx-b
    alist = new_alist[:idx2] + [c for _ in range(b)] + new_alist[idx2:]
  
print(sum(alist))

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

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

n, m = map(int, input().split())

alist = sorted(list(map(int, input().split())))

for _ in range(m):
  b, c = map(int, input().split())
  idx = bisect.bisect_right(alist,c)
  if idx-1 <= b:
    alist = [c for _ in range(idx)] + alist[idx:]
  else:
    new_alist = alist[b:]
    idx2 = bisect.bisect_right(new_alist,c)
    alist = new_alist[:idx2] + [c for _ in range(b)] + new_alist[idx2:]
  
print(sum(alist))

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)

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

n, m = map(int, input().split())

alist = sorted(list(map(int, input().split())))

for _ in range(m):
  b, c = map(int, input().split())
  idx = bisect.bisect_right(alist,c)
  if idx-1 <= b:
    alist = [c for _ in range(idx)] + alist[idx:]
  else:
    alist = [c for _ in range(b)] + alist[b:idx] + alist[idx:]
  alist.sort() 
print(sum(alist))

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

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

num = 0
ans = 0 
for c,b in CB:
  for bi in range(b):
    if num >= N: break
    if num > N or c<=A[num]:
      break
    else:
      num += 1
      ans += c
print(ans+sum(A[num:]) if num != N else ans)