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

B_C = sorted(B_C, key=lambda x:x[1], reverse=True)

D = []
for j in range(M):
    D += [B_C[j][1]] * B_C[j][0]

A.sort()

for i in range(N):
    if A[i] < D[i]:
        A[i] = D[i]
    else:
        break
    if i == len(D) - 1:
        break

print(sum(A))

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

B_C = sorted(B_C, key=lambda x:x[1], reverse=True)

B = [B_C[j][0] for j in range(M)]
C = [B_C[j][1] for j in range(M)]

D = []
for j in range(M):
    D += [C[j]] * B[j]
    if len(D) >= N:
        break

A.sort()

for i in range(N):
    if A[i] < D[i]:
        A[i] = D[i]
    else:
        break
    if i == len(D) - 1:
        break

print(sum(A))

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

B_C = sorted(B_C, key=lambda x:x[1], reverse=True)

B = [B_C[j][0] for j in range(M)]
C = [B_C[j][1] for j in range(M)]

D = []
for j in range(M):
    D += [C[j]] * B[j]

A.sort()

for i in range(N):
    if A[i] < D[i]:
        A[i] = D[i]
    else:
        break
    if i == len(D) - 1:
        break

print(sum(A))

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

B_C = sorted(B_C, key=lambda x:x[1], reverse=True)

B = [B_C[j][0] for j in range(M)]
C = [B_C[j][1] for j in range(M)]

D = []
for j in range(M):
    D += [C[j]] * B[j]
    if len(D) >= N:
        break

A.sort()

for i in range(N):
    if A[i] < D[i]:
        A[i] = D[i]
    else:
        break
    if i == len(D) - 1:
        break

print(sum(A))

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

B_C = sorted(B_C, key=lambda x:x[1], reverse=True)

B = [B_C[j][0] for j in range(M)]
C = [B_C[j][1] for j in range(M)]

D = []
for j in range(M):
    D += [C[j] for k in range(B[j])]

A.sort()

for i in range(N):
    if A[i] < D[i]:
        A[i] = D[i]
    else:
        break
    if i == len(D) - 1:
        break

print(sum(A))

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

B_C = sorted(B_C, key=lambda x:x[1], reverse=True)

B = [B_C[j][0] for j in range(M)]
C = [B_C[j][1] for j in range(M)]

D = []
for j in range(M):
    D += [C[j]] * B[j]
    if len(D) >= N:
        break

A.sort()

for i in range(N):
    if A[i] < D[i]:
        A[i] = D[i]
    else:
        break
    if i == len(D) - 1:
        break

print(sum(A))

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

B = [B_C[j][0] for j in range(M)]
C = [B_C[j][1] for j in range(M)]

A.sort()
for j in range(M):
    for i in range(B[j]):
        if A[i] < C[j]:
            A[i] = C[j]
    A.sort()
print(sum(A))

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

B_C = sorted(B_C, key=lambda x:x[1], reverse=True)

B = [B_C[j][0] for j in range(M)]
C = [B_C[j][1] for j in range(M)]

D = []
for j in range(M):
    D += [C[j]] * B[j]
    if len(D) >= N:
        break

A.sort()

for i in range(N):
    if A[i] < D[i]:
        A[i] = D[i]
    else:
        break
    if i == len(D) - 1:
        break

print(sum(A))