Buggy python submission (verdict: Runtime Error):
import copy

N, M=map(int, input().split())
A=list(map(int, input().split()))
BC=[]
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
A=sorted(A)
BC=sorted(BC, key=lambda val:val[1], reverse=True)
#print(N,M,A,BC)


change=[]
for bc in BC:
    for _ in range(bc[0]):
        change.append(bc[1])
    if len(change)>=N:
        break
#print(change)

if A[0]>=change[0]:
    print(sum(A))
else:
    for i in range(len(change)):
        if A[i]>change[i]:
            answer = sum(change[:i]) + sum(A[i:])
            print(answer)
            break
    else:
        answer=sum(change[:i+1])+sum(A[i+1:])
        print(answer)


Fixed submission:
import copy

N, M=map(int, input().split())
A=list(map(int, input().split()))
BC=[]
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
A=sorted(A)
BC=sorted(BC, key=lambda val:val[1], reverse=True)
#print(N,M,A,BC)


change=[]
for bc in BC:
    for _ in range(bc[0]):
        change.append(bc[1])
    if len(change)>=N:
        break
#print(change)

if A[0]>=change[0]:
    print(sum(A))
else:
    for i in range(min(N,len(change))):
        if A[i]>change[i]:
            answer = sum(change[:i]) + sum(A[i:])
            print(answer)
            break
    else:
        answer=sum(change[:i+1])+sum(A[i+1:])
        print(answer)

Buggy python submission (verdict: Runtime Error):
import copy

N, M=map(int, input().split())
A=list(map(int, input().split()))
BC=[]
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
A=sorted(A)
BC=sorted(BC, key=lambda val:val[1], reverse=True)
#print(N,M,A,BC)


change=[]
for bc in BC:
    for _ in range(bc[0]):
        change.append(bc[1])
    if len(change)>=N:
        break
#print(change)


for i in range(len(change)):
    if A[i]>change[i]:
        answer = sum(change[:i]) + sum(A[i:])
        print(answer)
        break
else:
    answer=sum(change[:i+1])+sum(A[i+1:])
    print(answer)


Fixed submission:
import copy

N, M=map(int, input().split())
A=list(map(int, input().split()))
BC=[]
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
A=sorted(A)
BC=sorted(BC, key=lambda val:val[1], reverse=True)
#print(N,M,A,BC)


change=[]
for bc in BC:
    for _ in range(bc[0]):
        change.append(bc[1])
    if len(change)>=N:
        break
#print(change)

if A[0]>=change[0]:
    print(sum(A))
else:
    for i in range(min(N,len(change))):
        if A[i]>change[i]:
            answer = sum(change[:i]) + sum(A[i:])
            print(answer)
            break
    else:
        answer=sum(change[:i+1])+sum(A[i+1:])
        print(answer)

Buggy python submission (verdict: Runtime Error):
N, M=map(int, input().split())
A=list(map(int, input().split()))
BC=[]
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
A=sorted(A)
BC=sorted(BC, key=lambda val:val[1], reverse=True)
#print(N,M,A,BC)


change=[]
for bc in BC:
    for _ in range(bc[0]):
        change.append(bc[1])
#print(change)

for i in range(len(change)):
    if A[i]>change[i]:
        answer = sum(change[:i]) + sum(A[i:])
        print(answer)
        break
else:
    answer=sum(change[:i+1])+sum(A[i+1:])
    print(answer)


Fixed submission:
import copy

N, M=map(int, input().split())
A=list(map(int, input().split()))
BC=[]
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
A=sorted(A)
BC=sorted(BC, key=lambda val:val[1], reverse=True)
#print(N,M,A,BC)


change=[]
for bc in BC:
    for _ in range(bc[0]):
        change.append(bc[1])
    if len(change)>=N:
        break
#print(change)

if A[0]>=change[0]:
    print(sum(A))
else:
    for i in range(min(N,len(change))):
        if A[i]>change[i]:
            answer = sum(change[:i]) + sum(A[i:])
            print(answer)
            break
    else:
        answer=sum(change[:i+1])+sum(A[i+1:])
        print(answer)

Buggy python submission (verdict: Runtime Error):
N, M=map(int, input().split())
A=list(map(int, input().split()))
BC=[]
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
A=sorted(A)
BC=sorted(BC, key=lambda val:val[1], reverse=True)
print(N,M,A,BC)


change=[]
for bc in BC:
    for _ in range(bc[0]):
        change.append(bc[1])
print(change)

for i in range(len(change)):
    if A[i]>change[i]:
        answer = sum(change[:i]) + sum(A[i:])
        print(answer)
        break
else:
    answer=sum(change[:i+1])+sum(A[i+1:])
    print(answer)


Fixed submission:
import copy

N, M=map(int, input().split())
A=list(map(int, input().split()))
BC=[]
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
A=sorted(A)
BC=sorted(BC, key=lambda val:val[1], reverse=True)
#print(N,M,A,BC)


change=[]
for bc in BC:
    for _ in range(bc[0]):
        change.append(bc[1])
    if len(change)>=N:
        break
#print(change)

if A[0]>=change[0]:
    print(sum(A))
else:
    for i in range(min(N,len(change))):
        if A[i]>change[i]:
            answer = sum(change[:i]) + sum(A[i:])
            print(answer)
            break
    else:
        answer=sum(change[:i+1])+sum(A[i+1:])
        print(answer)