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

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

change=[tuple(map(int,input().split())) for _ in range(M)]
change=sorted(change,key=lambda x: x[1])
change2=[]
for i in range(M):
  b,c=change[i]
  change2.extend([c]*b)

change2 = change2[max(len(change2)-N,0):]

A.extend(change2)
A = sorted(A)
print(sum(A[len(A)-N:]))

Fixed submission:
from bisect import bisect_left

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

change=[tuple(map(int,input().split())) for _ in range(M)]
change=sorted(change,key=lambda x: x[1],reverse=True)
change2=[]
count=0
for i in range(M):
  b,c=change[i]
  change2.extend([c]*b)
  count += b
  if count >= N:break

A.extend(change2)
A = sorted(A)
print(sum(A[len(A)-N:]))

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

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

change=[tuple(map(int,input().split())) for _ in range(M)]
change=sorted(change,key=lambda x: x[1])
change2=[]
for i in range(M):
  b,c=change[i]
  change2.extend([c]*b)

change2 = change2[max(len(change2)-N,0):]

A.extend(change2)
A = sorted(A)
print(sum(A[len(A)-N:]))

Fixed submission:
from bisect import bisect_left

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

change=[tuple(map(int,input().split())) for _ in range(M)]
change=sorted(change,key=lambda x: x[1],reverse=True)
change2=[]
count=0
for i in range(M):
  b,c=change[i]
  change2.extend([c]*b)
  count += b
  if count >= N:break

A.extend(change2)
A = sorted(A)
print(sum(A[len(A)-N:]))

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

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

change=[tuple(map(int,input().split())) for _ in range(M)]
change=sorted(change,key=lambda x: x[1],reverse=True)

for i in range(M):
  b,c=change[i]
  idx = bisect_left(A,c)
  
  if idx-b>=0:
    A = A[b:idx]+[c]*b+A[idx:]
  else:
    A = [c]*idx+A[idx:]

print(sum(A))

Fixed submission:
from bisect import bisect_left

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

change=[tuple(map(int,input().split())) for _ in range(M)]
change=sorted(change,key=lambda x: x[1],reverse=True)
change2=[]
count=0
for i in range(M):
  b,c=change[i]
  change2.extend([c]*b)
  count += b
  if count >= N:break

A.extend(change2)
A = sorted(A)
print(sum(A[len(A)-N:]))

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

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

change=[tuple(map(int,input().split())) for _ in range(M)]
change=sorted(change,key=lambda x: x[1])

for i in range(M):
  b,c=change[i]
  idx = bisect_left(A,c)
  
  if idx-b>=0:
    A = A[b:idx]+[c]*b+A[idx:]
  else:
    A = [c]*idx+A[idx:]

print(sum(A))

Fixed submission:
from bisect import bisect_left

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

change=[tuple(map(int,input().split())) for _ in range(M)]
change=sorted(change,key=lambda x: x[1],reverse=True)
change2=[]
count=0
for i in range(M):
  b,c=change[i]
  change2.extend([c]*b)
  count += b
  if count >= N:break

A.extend(change2)
A = sorted(A)
print(sum(A[len(A)-N:]))