Buggy python submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
A_list = list(map(int, input().split()))

card = []
card_num = []

for a in A_list:
    card.append(a)
    card_num.append(1)
    
for _ in range(M):
    B, C = list(map(int, input().split()))
    card.append(C)
    card_num.append(B)
    
union = zip(card, card_num)
sorted_union = sorted(union, key=lambda x:x[0])[-1::-1]
sorted_card, sorted_card_num = zip(*sorted_union)a

ans = 0
current_num = 0
for c, c_num in zip(sorted_card, sorted_card_num):
    if current_num + c_num <= N:
        current_num += c_num
        ans += c * c_num
    else:
        current_num = N
        ans += c * (N - current_num)
print(ans)

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

card = []
card_num = []

for a in A_list:
    card.append(a)
    card_num.append(1)
        
for _ in range(M):
    B, C = list(map(int, input().split()))
    card.append(C)
    card_num.append(B)
    
union = zip(card, card_num)
sorted_union = sorted(union, key=lambda x:x[0])[-1::-1]
sorted_card, sorted_card_num = zip(*sorted_union)

ans = 0
current_num = 0
for c, c_num in zip(sorted_card, sorted_card_num):
    if current_num + c_num < N:
        current_num += c_num
        ans += c * c_num
    else:
        ans += c * (N - current_num)
        current_num = N
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
import numpy as np
N, M = list(map(int, input().split()))
A_list = list(map(int, input().split()))

card_num = np.zeros(10**9+1)
max_num = -1
for a in A_list:
    card_num[a] += 1
    if max_num < a:
        max_num = a

for _ in range(M):
    B, C = list(map(int, input().split()))
    card_num[C] += B
    if max_num < C:
        max_num = C

ans = 0
current_num = 0
for i in range(max_num, 0, -1):
    num = card_num[i]
    if num == 0:
        continue
    else:
        if current_num + num <= N:
            ans += num * i
            current_num += num
        else:
            ans += (N-current_num) * i
            break
print(int(ans))

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

card = []
card_num = []

for a in A_list:
    card.append(a)
    card_num.append(1)
        
for _ in range(M):
    B, C = list(map(int, input().split()))
    card.append(C)
    card_num.append(B)
    
union = zip(card, card_num)
sorted_union = sorted(union, key=lambda x:x[0])[-1::-1]
sorted_card, sorted_card_num = zip(*sorted_union)

ans = 0
current_num = 0
for c, c_num in zip(sorted_card, sorted_card_num):
    if current_num + c_num < N:
        current_num += c_num
        ans += c * c_num
    else:
        ans += c * (N - current_num)
        current_num = N
        break
print(ans)

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

cards = A_list
for _ in range(M):
    B, C = list(map(int, input().split()))
    cards.extend([C]*B)
ans = sum(sorted(cards)[-1::-1][:N])
print(ans)

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

card = []
card_num = []

for a in A_list:
    card.append(a)
    card_num.append(1)
        
for _ in range(M):
    B, C = list(map(int, input().split()))
    card.append(C)
    card_num.append(B)
    
union = zip(card, card_num)
sorted_union = sorted(union, key=lambda x:x[0])[-1::-1]
sorted_card, sorted_card_num = zip(*sorted_union)

ans = 0
current_num = 0
for c, c_num in zip(sorted_card, sorted_card_num):
    if current_num + c_num < N:
        current_num += c_num
        ans += c * c_num
    else:
        ans += c * (N - current_num)
        current_num = N
        break
print(ans)