Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = []
for i in range(M) :
    b,c = map(int,input().split())
    L.append([b,c])

L = sorted(L,key=lambda x: x[1], reverse=True)
for i in range(len(L)) :
    b,c = L[i]
    b = min(b,10**9-len(A))
    A.extend([c]*b)
ans = sum(sorted(A,reverse=True)[:N])

print(ans)

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = [list(map(int,input().split())) for i in range(M)]

P = sorted(([[1,A[i]] for i in range(N)] + L),key=lambda x: x[1],reverse=True)

ans = 0
rest = N

for cost,value in P :
    K = min(cost, rest)
    K = max(0,K)
    ans += K * value
    rest -= cost

print(ans)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = []
for i in range(M) :
    b,c = map(int,input().split())
    L.append(b,c)

L = sorted(L,key=lambda x: x[1], reverse=True)
for i in range(len(L)) :
    b,c = L[i]
    b = min(b,10**9-len(A))
    A.extend([c]*b)
ans = sum(sorted(A,reverse=True)[:N])

print(ans)

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = [list(map(int,input().split())) for i in range(M)]

P = sorted(([[1,A[i]] for i in range(N)] + L),key=lambda x: x[1],reverse=True)

ans = 0
rest = N

for cost,value in P :
    K = min(cost, rest)
    K = max(0,K)
    ans += K * value
    rest -= cost

print(ans)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = []
for i in range(M) :
    b,c = map(int,input().split())
    L.append(b,c)

L = sorted(L,key=x: x[1], reverse=True)
for i in range(len(L)) :
    b,c = L[i]
    b = min(b,10**9-len(A))
    A.extend([c]*b)
ans = sum(sorted(A,reverse=True)[:N])

print(ans)

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = [list(map(int,input().split())) for i in range(M)]

P = sorted(([[1,A[i]] for i in range(N)] + L),key=lambda x: x[1],reverse=True)

ans = 0
rest = N

for cost,value in P :
    K = min(cost, rest)
    K = max(0,K)
    ans += K * value
    rest -= cost

print(ans)

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)
ans = sum(sorted(A,reverse=True)[:N])

print(ans)

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = [list(map(int,input().split())) for i in range(M)]

P = sorted(([[1,A[i]] for i in range(N)] + L),key=lambda x: x[1],reverse=True)

ans = 0
rest = N

for cost,value in P :
    K = min(cost, rest)
    K = max(0,K)
    ans += K * value
    rest -= cost

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())))
L = [list(map(int,input().split())) for i in range(M)]
L = sorted(L, key=lambda x: x[1], reverse=True)

ans = 0

q = bisect.bisect(A,L[0][1])
ans += sum(A[q:])
A = A[:q]

for i in range(M) :
    b,c = L[i]
    
    bi = bisect.bisect(A,c)
    
    K = min(b,bi)
    ans += c*K
    A = A[K:]
ans += sum(A)
print(ans)
    

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = [list(map(int,input().split())) for i in range(M)]

P = sorted(([[1,A[i]] for i in range(N)] + L),key=lambda x: x[1],reverse=True)

ans = 0
rest = N

for cost,value in P :
    K = min(cost, rest)
    K = max(0,K)
    ans += K * value
    rest -= cost

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())))
L = [list(map(int,input().split())) for i in range(M)]
L = sorted(L, key=lambda x: x[1], reverse=True)

ans = 0

for i in range(M) :
    b,c = L[i]
    
    bi = bisect.bisect(A,c)
    
    K = min(b,bi)
    ans += c*K
    A = A[K:]
ans += sum(A)
print(ans)

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = [list(map(int,input().split())) for i in range(M)]

P = sorted(([[1,A[i]] for i in range(N)] + L),key=lambda x: x[1],reverse=True)

ans = 0
rest = N

for cost,value in P :
    K = min(cost, rest)
    K = max(0,K)
    ans += K * value
    rest -= cost

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())))
L = [list(map(int,input().split())) for i in range(M)]
L = sorted(L, key=lambda x: x[1], reverse=True)

ans = 0

for i in range(M) :
    b,c = L[i]
    
    bi = bisect.bisect(A,c)
    
    K = min(b,bi)
    ans += c*K
    A = A[K:]
ans += sum(A)
print(ans)
    

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = [list(map(int,input().split())) for i in range(M)]

P = sorted(([[1,A[i]] for i in range(N)] + L),key=lambda x: x[1],reverse=True)

ans = 0
rest = N

for cost,value in P :
    K = min(cost, rest)
    K = max(0,K)
    ans += K * value
    rest -= cost

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())))
L = [list(map(int,input().split())) for i in range(M)]
L = sorted(L, key=lambda x: x[1], reverse=False)

for i in range(M) :
    b,c = L[i]
    
    bi = bisect.bisect(A,c)
    
    K = min(b,bi)
    #print(K,[b,bi])
    A = [c]*K + A[K:]
    A = sorted(A)
    #print(A)
print(sum(A))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
L = [list(map(int,input().split())) for i in range(M)]

P = sorted(([[1,A[i]] for i in range(N)] + L),key=lambda x: x[1],reverse=True)

ans = 0
rest = N

for cost,value in P :
    K = min(cost, rest)
    K = max(0,K)
    ans += K * value
    rest -= cost

print(ans)