Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())

A=list(map(int,input().split()))
A.sort()
dc=[]

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

dc.sort(key=lambda x: x[1],reverse=True)
bf=False
i=0

for l in dc:
    B,C=l
    if bf:
        break
    while B>0:
        if A[i]<C and i<N:
            A[i]=C
            B-=1
            i+=1
        else:
            bf=True
            i+=1
            break

print(sum(A))


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

A=list(map(int,input().split()))
A.sort()
dc=[]

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

dc.sort(key=lambda x: x[1],reverse=True)
bf=False
i=0
for l in dc:
    B,C=l
    if bf:
        break
    while B>0:
        if i<N and A[i]<C:
            A[i]=C
            B-=1
            i+=1
        else:
            bf=True
            break


print(sum(A))

Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())

A=list(map(int,input().split()))
A.sort()
dc=[]

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

dc.sort(key=lambda x: x[1],reverse=True)
bf=False
i=0

for l in dc:
    B,C=l
    if bf or i>=N:
        break
    while B>0:
        if A[i]<C:
            A[i]=C
            B-=1
            i+=1
        else:
            bf=True
            i+=1
            break

print(sum(A))


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

A=list(map(int,input().split()))
A.sort()
dc=[]

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

dc.sort(key=lambda x: x[1],reverse=True)
bf=False
i=0
for l in dc:
    B,C=l
    if bf:
        break
    while B>0:
        if i<N and A[i]<C:
            A[i]=C
            B-=1
            i+=1
        else:
            bf=True
            break


print(sum(A))

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

A=list(map(int,input().split()))
A.sort()
dc=[]

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

dc.sort(key=lambda x: x[1],reverse=True)
bf=False
i=0
for l in dc:
    B,C=l
    while B>0:
        if i<N and A[i]<C:
            A[i]=C
            B-=1
            i+=1
        elif i<N:
            bf=True
            i+=1
            break
    if bf:
        break

print(sum(A))


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

A=list(map(int,input().split()))
A.sort()
dc=[]

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

dc.sort(key=lambda x: x[1],reverse=True)
bf=False
i=0
for l in dc:
    B,C=l
    if bf:
        break
    while B>0:
        if i<N and A[i]<C:
            A[i]=C
            B-=1
            i+=1
        else:
            bf=True
            break


print(sum(A))

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

A=list(map(int,input().split()))
A.sort()
dc=[]

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

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

i=0
for l in dc:
    B,C=l
    while B>0:
        if i<N and A[i]<C:
            A[i]=C
            B-=1
            i+=1
        elif i<N:
            i+=1
            break

print(sum(A))

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

A=list(map(int,input().split()))
A.sort()
dc=[]

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

dc.sort(key=lambda x: x[1],reverse=True)
bf=False
i=0
for l in dc:
    B,C=l
    if bf:
        break
    while B>0:
        if i<N and A[i]<C:
            A[i]=C
            B-=1
            i+=1
        else:
            bf=True
            break


print(sum(A))