Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq
N,M=map(int,input().split())
alist=list(map(int,input().split()))

cblist=[]
for i in range(M):
  B,C=map(int,input().split())
  cblist.append((C,B))
cblist.sort(reverse=True)

hq=[]
for a in alist:
  heapq.heappush(hq,a)
  
for C,B in cblist:
  for i in range(B):
    if C<hq[0]:
      print(sum(hq))
      sys.exit(0)
    else:
      heapq.heapreplace(hq,C)
else:
  print(sum(hq))

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

alist.sort()
cblist = []
for i in range(M):
  B,C=map(int,input().split())
  cblist.append((C,B))
  
cblist.sort(reverse=True)
#print(alist)
#print(cblist)

index = 0
for (C,B) in cblist:
  for i in range(index,index+B):
    if i == len(alist):
      break
    
    if alist[i] < C:
      alist[i] = C
    else:
      break
      
  index += B
  if index >= len(alist):
    break
  
#print(alist)
print(sum(alist))

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
alist=list(map(int,input().split()))

alist.sort()
cblist = []
for i in range(M):
  B,C=map(int,input().split())
  cblist.append((C,B))
  
cblist.sort(reverse=True)
#print(alist)
#print(cblist)

for (C,B) in cblist:
  for i in range(B):
    if alist[i] < C:
      alist[i] = C
    else:
      break
  
  alist.sort()
  
#print(alist)
print(sum(alist))


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

alist.sort()
cblist = []
for i in range(M):
  B,C=map(int,input().split())
  cblist.append((C,B))
  
cblist.sort(reverse=True)
#print(alist)
#print(cblist)

index = 0
for (C,B) in cblist:
  for i in range(index,index+B):
    if i == len(alist):
      break
    
    if alist[i] < C:
      alist[i] = C
    else:
      break
      
  index += B
  if index >= len(alist):
    break
  
#print(alist)
print(sum(alist))

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
alist=list(map(int,input().split()))

alist.sort()
cblist = []
for i in range(M):
  B,C=map(int,input().split())
  cblist.append((C,B))
  
cblist.sort(reverse=True)
#print(alist)
#print(cblist)

for (C,B) in cblist:
  for i in range(B):
    if alist[i] < C:
      alist[i] = C
    else:
      break
  
  alist.sort()
  
#print(alist)
print(sum(alist))

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

alist.sort()
cblist = []
for i in range(M):
  B,C=map(int,input().split())
  cblist.append((C,B))
  
cblist.sort(reverse=True)
#print(alist)
#print(cblist)

index = 0
for (C,B) in cblist:
  for i in range(index,index+B):
    if i == len(alist):
      break
    
    if alist[i] < C:
      alist[i] = C
    else:
      break
      
  index += B
  if index >= len(alist):
    break
  
#print(alist)
print(sum(alist))

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
alist=list(map(int,input().split()))

alist.sort()
cblist = []
for i in range(M):
  B,C=map(int,input().split())
  cblist.append((C,B))
  
cblist.sort(reverse=True)
#print(alist)
#print(cblist)

for (C,B) in cblist:
  for i in range(B):
    alist[i] = max(alist[i],C)
  
  alist.sort()
  
#print(alist)
print(sum(alist))

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

alist.sort()
cblist = []
for i in range(M):
  B,C=map(int,input().split())
  cblist.append((C,B))
  
cblist.sort(reverse=True)
#print(alist)
#print(cblist)

index = 0
for (C,B) in cblist:
  for i in range(index,index+B):
    if i == len(alist):
      break
    
    if alist[i] < C:
      alist[i] = C
    else:
      break
      
  index += B
  if index >= len(alist):
    break
  
#print(alist)
print(sum(alist))