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

A = list(map(int, input().split()))

candidate = []
candidate.extend(A)

for j in range(M):
    b, c = map(int, input().split())    
    candidate.extend([c] * b)        
    candidate = sorted(candidate, reverse=True)[:N]
        
print(sum(candidate))

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

A = list(map(int, input().split()))

BC = []

for j in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))
    
sorted_BC = sorted(BC, reverse=True, key=lambda item: item[1])

candidate = []
count = N
for bc in sorted_BC:
    b = bc[0]
    c = bc[1]
    if count >= 0:
        candidate.extend([c] * b)
        count = count - b
    else:
        break
    
candidate.extend(A)

print(sum(sorted(candidate, reverse=True)[:N]))

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

A = list(map(int, input().split()))

candidate = []
candidate.extend(A)

for j in range(M):
    b, c = map(int, input().split())
    
    if b > N:
        b = N

    if c >= min(candidate):
        candidate.extend([c] * b)
        
    candidate = sorted(candidate, reverse=True)[:N]
        
print(sum(candidate))

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

A = list(map(int, input().split()))

BC = []

for j in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))
    
sorted_BC = sorted(BC, reverse=True, key=lambda item: item[1])

candidate = []
count = N
for bc in sorted_BC:
    b = bc[0]
    c = bc[1]
    if count >= 0:
        candidate.extend([c] * b)
        count = count - b
    else:
        break
    
candidate.extend(A)

print(sum(sorted(candidate, reverse=True)[:N]))

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

A = list(map(int, input().split()))

candidate = []
candidate.extend(A)

for j in range(M):
    b, c = map(int, input().split())
    
    if b > N:
        b = N

    if c >= min(candidate):
        candidate.extend([c] * b)
        
sorted_candidate = sorted(candidate, reverse=True)

print(sum(sorted_candidate[:N]))

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

A = list(map(int, input().split()))

BC = []

for j in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))
    
sorted_BC = sorted(BC, reverse=True, key=lambda item: item[1])

candidate = []
count = N
for bc in sorted_BC:
    b = bc[0]
    c = bc[1]
    if count >= 0:
        candidate.extend([c] * b)
        count = count - b
    else:
        break
    
candidate.extend(A)

print(sum(sorted(candidate, reverse=True)[:N]))

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

A = list(map(int, input().split()))

candidate = []
candidate.extend(A)

for j in range(M):
    b, c = map(int, input().split())
    candidate.extend([c] * b)
        
sorted_candidate = sorted(candidate, reverse=True)

print(sum(sorted_candidate[:N]))

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

A = list(map(int, input().split()))

BC = []

for j in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))
    
sorted_BC = sorted(BC, reverse=True, key=lambda item: item[1])

candidate = []
count = N
for bc in sorted_BC:
    b = bc[0]
    c = bc[1]
    if count >= 0:
        candidate.extend([c] * b)
        count = count - b
    else:
        break
    
candidate.extend(A)

print(sum(sorted(candidate, reverse=True)[:N]))

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

A = list(map(int, input().split()))

candidate = []
candidate.extend(A)

for j in range(M):
    b, c = map(int, input().split())
    for j in range(b):
        candidate.append(c)
        
sorted_candidate = sorted(candidate, reverse=True)

print(sum(sorted_candidate[:N]))

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

A = list(map(int, input().split()))

BC = []

for j in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))
    
sorted_BC = sorted(BC, reverse=True, key=lambda item: item[1])

candidate = []
count = N
for bc in sorted_BC:
    b = bc[0]
    c = bc[1]
    if count >= 0:
        candidate.extend([c] * b)
        count = count - b
    else:
        break
    
candidate.extend(A)

print(sum(sorted(candidate, reverse=True)[:N]))

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

A = list(map(int, input().split()))

B = []
C = []

for j in range(M):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)
    
candidate = []
candidate.extend(A)

for i, b in enumerate(B):
    for j in range(b):
        candidate.append(C[i])
        
sorted_candidate = sorted(candidate, reverse=True)

print(sum(sorted_candidate[:N]))

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

A = list(map(int, input().split()))

BC = []

for j in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))
    
sorted_BC = sorted(BC, reverse=True, key=lambda item: item[1])

candidate = []
count = N
for bc in sorted_BC:
    b = bc[0]
    c = bc[1]
    if count >= 0:
        candidate.extend([c] * b)
        count = count - b
    else:
        break
    
candidate.extend(A)

print(sum(sorted(candidate, reverse=True)[:N]))