Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
 
ans = 0
b,c = BC.pop(0)
for i,a in enumerate(A):
    if b>=1:
        if c > a:
            ans += c
            b -= 1
        else:
            ans += a
    if b<=0:
        if len(BC)==0:
            ans += sum(A[i+1:])
            break
        else:
            b,c = BC.pop(0)
 
print(ans)

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
 
ans = 0
b,c = BC.pop(0)
for i,a in enumerate(A):
    if b>=1:
        if c > a:
            ans += c
            b -= 1
        else:
            ans += a
    if b<=0:
        if len(BC)==0:
            ans += sum(A[i+1:])
            break
        else:
            b,c = BC.pop(0)
 
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = [int(i) for i in input().split()]
A.sort(reverse=True)
C = []
for _ in range(M):
    b,c = map(int,input().split())
    for _ in range(b):
        C.append(c)
C.sort(reverse=True)
ans = []

for n in range(N+1):
    ans.append(sum(A[:n])+sum(C[:N-n]))

print(max(ans))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
 
ans = 0
b,c = BC.pop(0)
for i,a in enumerate(A):
    if b>=1:
        if c > a:
            ans += c
            b -= 1
        else:
            ans += a
    if b<=0:
        if len(BC)==0:
            ans += sum(A[i+1:])
            break
        else:
            b,c = BC.pop(0)
 
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = [int(i) for i in input().split()]
A.sort(reverse=True)
C = []
for _ in range(M):
    b,c = map(int,input().split())
    for _ in range(b):
        C.append(c)
C.sort(reverse=True)
ans = []

for n in range(N+1):
    ans.append(sum(A[:n])+sum(C[:N-n]))

print(max(ans))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
 
ans = 0
b,c = BC.pop(0)
for i,a in enumerate(A):
    if b>=1:
        if c > a:
            ans += c
            b -= 1
        else:
            ans += a
    if b<=0:
        if len(BC)==0:
            ans += sum(A[i+1:])
            break
        else:
            b,c = BC.pop(0)
 
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = [int(i) for i in input().split()]
A.sort()
BC = [[0]*2 for i in range(M)]

for m in range(M):
    BC[m][0],BC[m][1] = map(int,input().split())

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


for m in range(M):
    B,C = BC[m][0],BC[m][1]
    for i in range(B):
        if A[i] > C:
            break
        A[i] = C
    A.sort()
 
print(sum(A))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
 
ans = 0
b,c = BC.pop(0)
for i,a in enumerate(A):
    if b>=1:
        if c > a:
            ans += c
            b -= 1
        else:
            ans += a
    if b<=0:
        if len(BC)==0:
            ans += sum(A[i+1:])
            break
        else:
            b,c = BC.pop(0)
 
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = [int(i) for i in input().split()]
A.sort()

 
for m in range(M):
    B,C = map(int,input().split())
    for i in range(B):
        if A[i] > C:
            break
        A[i] = C
    A.sort()
 
print(sum(A))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
 
ans = 0
b,c = BC.pop(0)
for i,a in enumerate(A):
    if b>=1:
        if c > a:
            ans += c
            b -= 1
        else:
            ans += a
    if b<=0:
        if len(BC)==0:
            ans += sum(A[i+1:])
            break
        else:
            b,c = BC.pop(0)
 
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = [int(i) for i in input().split()]
A.sort()

for m in range(M):
    B,C = map(int,input().split())
    for i in range(B):
        if A[i] > C:
            break
        A[i] = C
    A.sort()

print(sum(A))


Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
 
ans = 0
b,c = BC.pop(0)
for i,a in enumerate(A):
    if b>=1:
        if c > a:
            ans += c
            b -= 1
        else:
            ans += a
    if b<=0:
        if len(BC)==0:
            ans += sum(A[i+1:])
            break
        else:
            b,c = BC.pop(0)
 
print(ans)