Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import merge
N, M = map(int, sys.stdin.readline().split())
A = sorted([-i for i in list(map(int, sys.stdin.readline().split()))])
BC = sorted([list(map(int, sys.stdin.readline().split())) for _ in range(M)], key=lambda x: x[1], reverse=True)

card_list1 = []
for i in range(M):
    tmp_list = [-1 * BC[i][1]] * BC[i][0] 
    card_list1.extend(tmp_list)

sorted_cards = list(merge(A, card_list1))
ans = sum(sorted_cards[:N]) * -1
print(ans)

Fixed submission:
import sys
N, M = map(int, sys.stdin.readline().split())
A = [[1, i] for i in list(map(int, sys.stdin.readline().split()))]
BC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

card_list = A + BC
sorted_cards = sorted(card_list, key=lambda x: x[1], reverse=True)
n = 0
cards_sum = 0
for i in range(N):
    if sorted_cards[i][0] + n <= N:
        cards_sum += sorted_cards[i][1] * sorted_cards[i][0]
        n += sorted_cards[i][0]
    elif n < N and sorted_cards[i][0] + n > N:
        cards_sum += sorted_cards[i][1] * (N - n)
        n = N
        break
    else:
        break

print(cards_sum)

Buggy python submission (verdict: Runtime Error):
import sys
N, M = map(int, sys.stdin.readline().split())
A = sorted(list(map(int, sys.stdin.readline().split())))
B = []
C = []
for _ in range(M):
    b, c = map(int, sys.stdin.readline().split())
    B.append(b)
    C.append(c)

card_list = [0] * (N + sum(B))
card_list[:N] = A
start = N
for i in range(M):
    list = [C[i]] * B[i]
    card_list[start:start + len(list)] = list
    start = start + len(list)
    
sorted_cards = sorted(card_list, reverse=True)
ans = sum(sorted_cards[:N])
print(ans)

Fixed submission:
import sys
N, M = map(int, sys.stdin.readline().split())
A = [[1, i] for i in list(map(int, sys.stdin.readline().split()))]
BC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

card_list = A + BC
sorted_cards = sorted(card_list, key=lambda x: x[1], reverse=True)
n = 0
cards_sum = 0
for i in range(N):
    if sorted_cards[i][0] + n <= N:
        cards_sum += sorted_cards[i][1] * sorted_cards[i][0]
        n += sorted_cards[i][0]
    elif n < N and sorted_cards[i][0] + n > N:
        cards_sum += sorted_cards[i][1] * (N - n)
        n = N
        break
    else:
        break

print(cards_sum)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N, M = map(int, sys.stdin.readline().split())
A = list(map(int, sys.stdin.readline().split()))
BC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

card_list = A
for i in range(M):
    list = [BC[i][1]] * BC[i][0] 
    card_list.extend(list)

sorted_cards = sorted(card_list, reverse=True)
ans = sum(sorted_cards[:N])
print(ans)

Fixed submission:
import sys
N, M = map(int, sys.stdin.readline().split())
A = [[1, i] for i in list(map(int, sys.stdin.readline().split()))]
BC = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

card_list = A + BC
sorted_cards = sorted(card_list, key=lambda x: x[1], reverse=True)
n = 0
cards_sum = 0
for i in range(N):
    if sorted_cards[i][0] + n <= N:
        cards_sum += sorted_cards[i][1] * sorted_cards[i][0]
        n += sorted_cards[i][0]
    elif n < N and sorted_cards[i][0] + n > N:
        cards_sum += sorted_cards[i][1] * (N - n)
        n = N
        break
    else:
        break

print(cards_sum)