Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=list(map(int,input().split()))
tmp,count,l0=A[0],1,[]
for i in range(1,N):
  if tmp==A[i]:
    count+=1
  else:
    l0.append([tmp,count])
    tmp=A[i]
    count=1
l0.append([tmp,count])
BC=[]
for i in range(M):
  BC.append(list(map(int,input().split())))
BC.sort(key=lambda x:x[1],reverse=True)
cnt=0
l1=[]
for b,c in BC:
  if cnt>N:
    break
  else:
    l1.append([c,b])
    cnt+=b
l=l0+l1
l.sort(key=lambda x:x[0],reverse=True)
d=0
ans=0
while d<N:
  if l[0][1]==0:
    l.remove(l[0])
  ans+=l[0][0]
  d+=1
  l[0][1]-=1
print(ans)

Fixed submission:
N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[]
for i in range(M):
  BC.append(list(map(int,input().split())))
A.sort(reverse=True)
BC.sort(key=lambda x:x[1],reverse=True)
ans=sum(A)
for i in range(M):
  for _ in range(BC[i][0]):
    if A==[]:
      print(ans)
      exit()
    if A[-1]>BC[i][1]:
      print(ans)
      exit()
    else:
      p=A.pop()
      ans-=p
      ans+=BC[i][1]
      
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
tmp,count,l0=A[0],1,[]
for i in range(1,N):
  if tmp==A[i]:
    count+=1
  else:
    l0.append([tmp,count])
    tmp=A[i]
    count=1
l0.append([tmp,count])
BC=[]
for i in range(M):
  BC.append(list(map(int,input().split())))
BC.sort(key=lambda x:x[1],reverse=True)
cnt=0
l1=[]
for b,c in BC:
  if cnt>N:
    break
  else:
    l1.append([c,b])
    cnt+=b
l=l0+l1
l.sort(key=lambda x:x[0],reverse=True)
d=0
ans=0
while d<N:
  if l[0][1]==0:
    l.remove(l[0])
  ans+=l[0][0]
  d+=1
  l[0][1]-=1
print(ans)

Fixed submission:
N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[]
for i in range(M):
  BC.append(list(map(int,input().split())))
A.sort(reverse=True)
BC.sort(key=lambda x:x[1],reverse=True)
ans=sum(A)
for i in range(M):
  for _ in range(BC[i][0]):
    if A==[]:
      print(ans)
      exit()
    if A[-1]>BC[i][1]:
      print(ans)
      exit()
    else:
      p=A.pop()
      ans-=p
      ans+=BC[i][1]
      
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=list(map(int,input().split()))
for i in range(M):
  b,c=map(int,input().split())
  for _ in range(b):
    A.append(c)
ans=0
A.sort(reverse=True)
for i in range(N):
  ans+=A[i]
print(ans)

Fixed submission:
N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[]
for i in range(M):
  BC.append(list(map(int,input().split())))
A.sort(reverse=True)
BC.sort(key=lambda x:x[1],reverse=True)
ans=sum(A)
for i in range(M):
  for _ in range(BC[i][0]):
    if A==[]:
      print(ans)
      exit()
    if A[-1]>BC[i][1]:
      print(ans)
      exit()
    else:
      p=A.pop()
      ans-=p
      ans+=BC[i][1]
      
print(ans)

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



Fixed submission:
N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[]
for i in range(M):
  BC.append(list(map(int,input().split())))
A.sort(reverse=True)
BC.sort(key=lambda x:x[1],reverse=True)
ans=sum(A)
for i in range(M):
  for _ in range(BC[i][0]):
    if A==[]:
      print(ans)
      exit()
    if A[-1]>BC[i][1]:
      print(ans)
      exit()
    else:
      p=A.pop()
      ans-=p
      ans+=BC[i][1]
      
print(ans)