Buggy python submission (verdict: Time Limit Exceeded):
# 未完成　要素と個数を別にしてソートすればいける？
import numpy as np
N, M = map(int, input().split())
A = sorted([int(x) for x in input().split()])
card = np.zeros(N)
for i in range(N):
    card[i] = A[i]

for j in range(M):
    B, C = map(int, input().split())
    for k in range(B):
        card = np.append(card, C)
card = sorted(card, reverse=True)

ans = 0
for h in range(N):
    ans += card[h]
print("{}".format(int(ans)))


Fixed submission:
# 未完成　要素と個数を別にしてソートすればいける？
N, M = map(int, input().split())
A = [int(x) for x in input().split()]
cardnumberlist = []
card_dict = {}
for i in range(N):
    if A[i] in card_dict:
        card_dict[A[i]] += 1
    else:
        card_dict[A[i]] = 1
        cardnumberlist.append(A[i])

for j in range(M):
    B, C = map(int, input().split())
    if C in card_dict:
        card_dict[C] += B
    else:
        card_dict[C] = B
        cardnumberlist.append(C)
ans = 0
cardnumberlist = sorted(cardnumberlist, reverse=True)
# print(cardnumberlist)
# print(card_dict)
numofcard = 0
for h in range(N):
    #print(h)
    if numofcard + card_dict[cardnumberlist[h]] > N:
        ans += cardnumberlist[h] * (N - numofcard)
        break
    else:
        ans += cardnumberlist[h] * card_dict[cardnumberlist[h]]
        numofcard += card_dict[cardnumberlist[h]]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N, M = map(int, input().split())
A = sorted([int(x) for x in input().split()])
card = np.zeros(N)
for i in range(N):
    card[i] = A[i]

for j in range(M):
    B, C = map(int, input().split())
    for k in range(B):
        card = np.append(card, C)
card = sorted(card, reverse=True)

ans = 0
for h in range(N):
    ans += card[h]
print("{}".format(int(ans)))


Fixed submission:
# 未完成　要素と個数を別にしてソートすればいける？
N, M = map(int, input().split())
A = [int(x) for x in input().split()]
cardnumberlist = []
card_dict = {}
for i in range(N):
    if A[i] in card_dict:
        card_dict[A[i]] += 1
    else:
        card_dict[A[i]] = 1
        cardnumberlist.append(A[i])

for j in range(M):
    B, C = map(int, input().split())
    if C in card_dict:
        card_dict[C] += B
    else:
        card_dict[C] = B
        cardnumberlist.append(C)
ans = 0
cardnumberlist = sorted(cardnumberlist, reverse=True)
# print(cardnumberlist)
# print(card_dict)
numofcard = 0
for h in range(N):
    #print(h)
    if numofcard + card_dict[cardnumberlist[h]] > N:
        ans += cardnumberlist[h] * (N - numofcard)
        break
    else:
        ans += cardnumberlist[h] * card_dict[cardnumberlist[h]]
        numofcard += card_dict[cardnumberlist[h]]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N, M = map(int, input().split())
A = sorted([int(x) for x in input().split()])
card = np.zeros(N*2)
for i in range(N):
    card[i] = A[i]

for j in range(M):
    B, C = map(int, input().split())
    for k in range(B):
        if card[N+k] < C:
            card[N+k] = C
        else:
            break
    card = sorted(card, reverse=True)

ans = 0
for h in range(N):
    ans += card[h]
print("{}".format(int(ans)))


Fixed submission:
# 未完成　要素と個数を別にしてソートすればいける？
N, M = map(int, input().split())
A = [int(x) for x in input().split()]
cardnumberlist = []
card_dict = {}
for i in range(N):
    if A[i] in card_dict:
        card_dict[A[i]] += 1
    else:
        card_dict[A[i]] = 1
        cardnumberlist.append(A[i])

for j in range(M):
    B, C = map(int, input().split())
    if C in card_dict:
        card_dict[C] += B
    else:
        card_dict[C] = B
        cardnumberlist.append(C)
ans = 0
cardnumberlist = sorted(cardnumberlist, reverse=True)
# print(cardnumberlist)
# print(card_dict)
numofcard = 0
for h in range(N):
    #print(h)
    if numofcard + card_dict[cardnumberlist[h]] > N:
        ans += cardnumberlist[h] * (N - numofcard)
        break
    else:
        ans += cardnumberlist[h] * card_dict[cardnumberlist[h]]
        numofcard += card_dict[cardnumberlist[h]]
print(ans)