Buggy python submission (verdict: Time Limit Exceeded):
def main(A,D,N):
    K=min(N,len(D))
    for i in range(K):
        if A[i]>D[i]:
            return sum(D[:i]+A[i:])
    return sum(D[:K]+A[K:])

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
CB=[]
for m in range(M):
    b,c=map(int,input().split())
    CB.append([c,b])
CB.sort()
CB.reverse()
D=[]
while len(D)<N and CB!=[]:
    D.append(CB[0][0])
    CB[0][1]-=1
    if CB[0][1]==0:
        CB.remove(CB[0])
for cb in CB:
    D+=[cb[0]]*cb[1]
print(main(A,D,N))

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

BC=[]
for m in range(M):
    b,c=map(int,input().split())
    BC.append([b,c])

A.sort()
BC.sort(key=lambda x:x[1],reverse=True)

D=[]
for [b,c] in BC:
    D+=[c]*b
    if len(D)>N:
        break

if len(D)<N:
    D+=[0]*(N-len(D))

AD=[max(A[i],D[i]) for i in range(N)]
 
print(sum(AD))

Buggy python submission (verdict: Runtime Error):
def main(A,D,N):
    K=min(N,len(D))
    for i in range(K):
        if A[i]>D[i]:
            return sum(D[:i]+A[i:])
    return sum(D[:K]+A[K:])

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
CB=[]
for m in range(M):
    b,c=map(int,input().split())
    CB.append([c,b])
CB.sort()
CB.reverse()
D=[]
while len(D)<N and CB!=[]:
    D.append(CB[0][0])
    CB[0][1]-=1
    if CB[0][1]==0:
        CB.remove(CB[0])
for cb in CB:
    B+=[cb[0]]*cb[1]
print(main(A,D,N))

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

BC=[]
for m in range(M):
    b,c=map(int,input().split())
    BC.append([b,c])

A.sort()
BC.sort(key=lambda x:x[1],reverse=True)

D=[]
for [b,c] in BC:
    D+=[c]*b
    if len(D)>N:
        break

if len(D)<N:
    D+=[0]*(N-len(D))

AD=[max(A[i],D[i]) for i in range(N)]
 
print(sum(AD))

Buggy python submission (verdict: Runtime Error):
def main(A,D,N):
    K=min(N,len(D))
    for i in range(K):
        if A[i]>D[i]:
            return sum(D[:i]+A[i:])
    return sum(D[:K]+A[K:])

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
CB=[]
for m in range(M):
    b,c=map(int,input().split())
    CB.append([c,b])
CB.sort()
CB.reverse()
D=[]
while len(D)<N and CB!=[]:
    D.append(CB[0][0])
    CB[0][1]-=1
    if CB[0][1]==0:
        CB.remove(CB[0])
for bc in BC:
    B+=[bc[0]]*bc[1]
print(main(A,D,N))

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

BC=[]
for m in range(M):
    b,c=map(int,input().split())
    BC.append([b,c])

A.sort()
BC.sort(key=lambda x:x[1],reverse=True)

D=[]
for [b,c] in BC:
    D+=[c]*b
    if len(D)>N:
        break

if len(D)<N:
    D+=[0]*(N-len(D))

AD=[max(A[i],D[i]) for i in range(N)]
 
print(sum(AD))

Buggy python submission (verdict: Runtime Error):
def main(A,D,N):
    for i in range(N):
        if A[i]>D[i]:
            return sum(D[:i]+A[i:])
    return sum(D)

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
CB=[]
for m in range(M):
    b,c=map(int,input().split())
    CB.append([c,b])
CB.sort()
CB=CB[::-1]
D=[]
while len(D)<N:
    D.append(CB[0][0])
    CB[0][1]-=1
    if CB[0][1]==0:
        CB.remove(CB[0])
for bc in BC:
    B+=[bc[0]]*bc[1]
print(main(A,B,N))

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

BC=[]
for m in range(M):
    b,c=map(int,input().split())
    BC.append([b,c])

A.sort()
BC.sort(key=lambda x:x[1],reverse=True)

D=[]
for [b,c] in BC:
    D+=[c]*b
    if len(D)>N:
        break

if len(D)<N:
    D+=[0]*(N-len(D))

AD=[max(A[i],D[i]) for i in range(N)]
 
print(sum(AD))

Buggy python submission (verdict: Time Limit Exceeded):
def main(A,B,N):
    K=len(B)
    R=min(N,K)
    for i in range(R):
        if A[i]>B[i]:
            return sum(B[:i]+A[i:])
    return sum(B[:R]+A[R:])

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
BC=[]
for m in range(M):
    b,c=map(int,input().split())
    BC.append([c,b])
BC.sort()
BC=BC[::-1]
B=[]
for bc in BC:
    B+=[bc[0]]*bc[1]
print(main(A,B,N))

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

BC=[]
for m in range(M):
    b,c=map(int,input().split())
    BC.append([b,c])

A.sort()
BC.sort(key=lambda x:x[1],reverse=True)

D=[]
for [b,c] in BC:
    D+=[c]*b
    if len(D)>N:
        break

if len(D)<N:
    D+=[0]*(N-len(D))

AD=[max(A[i],D[i]) for i in range(N)]
 
print(sum(AD))

Buggy python submission (verdict: Time Limit Exceeded):
def main(A,B,N):
    A=sorted(A)
    B=sorted(B)[::-1]
    K=len(B)
    R=min(N,K)
    for i in range(R):
        if A[i]>B[i]:
            return sum(B[:i]+A[i:])
    return sum(B[:R]+A[R:])

N,M=map(int,input().split())
A=list(map(int,input().split()))
B=[]
for m in range(M):
    b,c=map(int,input().split())
    B+=[c]*b

print(main(A,B,N))

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

BC=[]
for m in range(M):
    b,c=map(int,input().split())
    BC.append([b,c])

A.sort()
BC.sort(key=lambda x:x[1],reverse=True)

D=[]
for [b,c] in BC:
    D+=[c]*b
    if len(D)>N:
        break

if len(D)<N:
    D+=[0]*(N-len(D))

AD=[max(A[i],D[i]) for i in range(N)]
 
print(sum(AD))

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=list(map(int,input().split()))
B=[]
for m in range(M):
    b,c=map(int,input().split())
    B+=[c]*b
A=sorted(A)
B=sorted(B)[::-1]
Max=0
for i in range(N+1):
    New=sum(A[i:])+sum(B[:i])
    if New>Max:
        Max=New
print(Max)

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

BC=[]
for m in range(M):
    b,c=map(int,input().split())
    BC.append([b,c])

A.sort()
BC.sort(key=lambda x:x[1],reverse=True)

D=[]
for [b,c] in BC:
    D+=[c]*b
    if len(D)>N:
        break

if len(D)<N:
    D+=[0]*(N-len(D))

AD=[max(A[i],D[i]) for i in range(N)]
 
print(sum(AD))