Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A_array = list(map(int, input().split()))
BC_array = [list(map(int, input().split())) for _ in range(M)]
BC_array = sorted(BC_array, key=lambda x: -x[1])
B_count = 0
for B, C in BC_array:
    A_array = A_array + [C] * B
    B_count += B
    if B_count >= N:
        break
A_array.sort()
# print(A_array)
print(sum(A_array[-N:]))

Fixed submission:
N, M = map(int, input().split())
A_array = sorted(map(int, input().split()))
BC_array = [list(map(int, input().split())) for _ in range(M)]
BC_array = sorted(BC_array, key=lambda x: x[1])

plus_count = 0
ans = 0
a_index = N - 1
bc_index = M - 1

while plus_count != N:
    if A_array[a_index] <= BC_array[bc_index][1]:
        add_count = min(BC_array[bc_index][0], N - plus_count)
        plus_count += add_count
        ans += (add_count * BC_array[bc_index][1])
        bc_index -= 1
        if bc_index < 0:
            a_rest = N - plus_count
            ans += sum(A_array[a_index - a_rest + 1:a_index + 1])
            break
    else:
        plus_count += 1
        ans += A_array[a_index]
        a_index -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A_array = list(map(int, input().split()))
BC_array = [list(map(int, input().split())) for _ in range(M)]
BC_array = sorted(BC_array, key=lambda x: -x[1])
B_count = 0
for B, C in BC_array:
    A_array = A_array + [C] * B
    B_count += B
    if B_count >= N:
        break
A_array.sort()
# print(A_array)
print(sum(A_array[-N:]))

Fixed submission:
N, M = map(int, input().split())
A_array = sorted(map(int, input().split()))
BC_array = [list(map(int, input().split())) for _ in range(M)]
BC_array = sorted(BC_array, key=lambda x: x[1])

plus_count = 0
ans = 0
a_index = N - 1
bc_index = M - 1

while plus_count != N:
    if A_array[a_index] <= BC_array[bc_index][1]:
        add_count = min(BC_array[bc_index][0], N - plus_count)
        plus_count += add_count
        ans += (add_count * BC_array[bc_index][1])
        bc_index -= 1
        if bc_index < 0:
            a_rest = N - plus_count
            ans += sum(A_array[a_index - a_rest + 1:a_index + 1])
            break
    else:
        plus_count += 1
        ans += A_array[a_index]
        a_index -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A_array = list(map(int, input().split()))
A_array.sort()
BC_array = [list(map(int, input().split())) for _ in range(M)]
BC_array = sorted(BC_array, key=lambda x: x[1])
# print(A_array, BC_array)

for B, C in BC_array:
    for i in range(B):
        if A_array[i] < C:
            A_array[i] = C
        else:
            break
    A_array.sort()

print(sum(A_array))

Fixed submission:
N, M = map(int, input().split())
A_array = sorted(map(int, input().split()))
BC_array = [list(map(int, input().split())) for _ in range(M)]
BC_array = sorted(BC_array, key=lambda x: x[1])

plus_count = 0
ans = 0
a_index = N - 1
bc_index = M - 1

while plus_count != N:
    if A_array[a_index] <= BC_array[bc_index][1]:
        add_count = min(BC_array[bc_index][0], N - plus_count)
        plus_count += add_count
        ans += (add_count * BC_array[bc_index][1])
        bc_index -= 1
        if bc_index < 0:
            a_rest = N - plus_count
            ans += sum(A_array[a_index - a_rest + 1:a_index + 1])
            break
    else:
        plus_count += 1
        ans += A_array[a_index]
        a_index -= 1
print(ans)