Buggy python submission (verdict: Time Limit Exceeded):
#7  Integer Cards
n,m = map(int,input().split())
a = list( map(int,input().split()))
bc=[[0]*2 for _ in range(m)]
for i in range(m):
    bc[i] = list(map(int,input().split()))

a.sort()
bc.sort(reverse=True,key=lambda x:x[1])

import bisect
ans=[]
for (bb,cc) in bc:
    idx = bisect.bisect(a,cc)
    n_replace = min(idx,bb)
    ans.extend([cc]*n_replace)
    a=a[n_replace:]
    
print(sum(ans)+sum(a))


Fixed submission:
n,m=map(int,input().split())
a = list(map(int, input().split()))
bc=[list(map(int,input().split())) for _ in range(m)]
#print(bc)
bc=sorted(bc,key= lambda x:x[1],reverse=True)
#print(bc)

bc2=[]
for i in range(m):
    bc2.extend([bc[i][1]]*bc[i][0])
    if len(bc2)>n: break
#print(bc2)
a.extend(bc2)
a.sort(reverse=True)

print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a = list(map(int, input().split()))

d=[]
for i in range(m):
    b,c = map(int, input().split())
    d.extend([c]*b)    
d.sort(reverse=True)

if len(d)>n:d=d[:n]

a.extend(d)
a.sort(reverse=True)

print(sum(a[:n]))

Fixed submission:
n,m=map(int,input().split())
a = list(map(int, input().split()))
bc=[list(map(int,input().split())) for _ in range(m)]
#print(bc)
bc=sorted(bc,key= lambda x:x[1],reverse=True)
#print(bc)

bc2=[]
for i in range(m):
    bc2.extend([bc[i][1]]*bc[i][0])
    if len(bc2)>n: break
#print(bc2)
a.extend(bc2)
a.sort(reverse=True)

print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a = list(map(int, input().split()))
a.sort()

d=[]
for i in range(m):
    b,c = map(int, input().split())
    d.extend([c]*b)    
d.sort(reverse=True)

for i in range(min(n,len(d))):
    if a[i]<d[i]: a[i]=d[i]

print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a = list(map(int, input().split()))
bc=[list(map(int,input().split())) for _ in range(m)]
#print(bc)
bc=sorted(bc,key= lambda x:x[1],reverse=True)
#print(bc)

bc2=[]
for i in range(m):
    bc2.extend([bc[i][1]]*bc[i][0])
    if len(bc2)>n: break
#print(bc2)
a.extend(bc2)
a.sort(reverse=True)

print(sum(a[:n]))

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())
    a.extend([c]*b)    
a.sort(reverse=True)
print(sum(a[:n]))

Fixed submission:
n,m=map(int,input().split())
a = list(map(int, input().split()))
bc=[list(map(int,input().split())) for _ in range(m)]
#print(bc)
bc=sorted(bc,key= lambda x:x[1],reverse=True)
#print(bc)

bc2=[]
for i in range(m):
    bc2.extend([bc[i][1]]*bc[i][0])
    if len(bc2)>n: break
#print(bc2)
a.extend(bc2)
a.sort(reverse=True)

print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a = list(map(int, input().split()))
b=[0]*m
c=[0]*m
for i in range(m):
    b[i],c[i] = map(int, input().split())

for i in range(m):
    for _ in range(b[i]):
        a.append(c[i])
a.sort(reverse=True)

print(sum(a[:n]))

Fixed submission:
n,m=map(int,input().split())
a = list(map(int, input().split()))
bc=[list(map(int,input().split())) for _ in range(m)]
#print(bc)
bc=sorted(bc,key= lambda x:x[1],reverse=True)
#print(bc)

bc2=[]
for i in range(m):
    bc2.extend([bc[i][1]]*bc[i][0])
    if len(bc2)>n: break
#print(bc2)
a.extend(bc2)
a.sort(reverse=True)

print(sum(a[:n]))