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

g_cards = list(map(int, input().split()))

# 辞書データで管理
dict_cards = {}
for card in g_cards:
	if(card in dict_cards):
		dict_cards[card] += 1
	else:
		dict_cards[card] = 1
# 操作
for i in range(M):
	# sizeと同じ枚数のカードが増えたと考える
	size, val = map(int, input().split())
	if(val in dict_cards):
		dict_cards[val] += size
	else:
		dict_cards[val] = size

# 昇順にする
s_dict_cards = sorted(dict_cards.items(), key=lambda x: -x[0])

# 取り出す
s_cards = []
for val, cnt in s_dict_cards:
	s_cards += [val] * cnt
	# # 始めの個数を超えたら抜ける
	# if(len(s_cards) >= N):
	# 	break
# 出力
sum_val = sum(s_cards[:N])
print(sum_val)

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

g_cards = list(map(int, input().split()))

# key-valueデータで管理
dict_cards = []
for card in g_cards:
	dict_cards.append([card, 1])

# 操作
for i in range(M):
	# sizeと同じ枚数のカードが増えたと考える
	size, val = map(int, input().split())
	dict_cards.append([val, size])

# 降順にする
s_dict_cards = sorted(dict_cards, key=lambda x: -x[0])

s_cards = []
for data in s_dict_cards:
	s_cards += [data[0]] * data[1]
	if(len(s_cards) >= N):
		break
# 出力
sum_val = sum(s_cards[:N])
print(sum_val)

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

g_cards = list(map(int, input().split()))

# 辞書データで管理
dict_cards = {}
for card in g_cards:
	if(card in dict_cards):
		dict_cards[card] += 1
	else:
		dict_cards[card] = 1
# 操作
for i in range(M):
	# sizeと同じ枚数のカードが増えたと考える
	size, val = map(int, input().split())
	if(val in dict_cards):
		dict_cards[val] += size
	else:
		dict_cards[val] = size

# 昇順にする
s_dict_cards = sorted(dict_cards.items(), key=lambda  x: -x[0])

# 取り出す
s_cards = []
for val, cnt in s_dict_cards:
	s_cards += [val] * cnt
	# 始めの個数を超えたら抜ける
	if(len(s_cards) >= N):
		break
# 出力
sum_val = sum(s_cards[:N])
print(sum_val)

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

g_cards = list(map(int, input().split()))

# key-valueデータで管理
dict_cards = []
for card in g_cards:
	dict_cards.append([card, 1])

# 操作
for i in range(M):
	# sizeと同じ枚数のカードが増えたと考える
	size, val = map(int, input().split())
	dict_cards.append([val, size])

# 降順にする
s_dict_cards = sorted(dict_cards, key=lambda x: -x[0])

s_cards = []
for data in s_dict_cards:
	s_cards += [data[0]] * data[1]
	if(len(s_cards) >= N):
		break
# 出力
sum_val = sum(s_cards[:N])
print(sum_val)

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

# 昇順のカードとする
g_cards = list(map(int, input().split()))

# 操作
for i in range(M):
	# sizeと同じ枚数の値が増えたと考える
	size, val = map(int, input().split())
	for i in range(size):
		g_cards.append(val)

# ソート
cards = sorted(g_cards, reverse=True)

sum_val = sum(cards[:N])

print(sum_val)

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

g_cards = list(map(int, input().split()))

# key-valueデータで管理
dict_cards = []
for card in g_cards:
	dict_cards.append([card, 1])

# 操作
for i in range(M):
	# sizeと同じ枚数のカードが増えたと考える
	size, val = map(int, input().split())
	dict_cards.append([val, size])

# 降順にする
s_dict_cards = sorted(dict_cards, key=lambda x: -x[0])

s_cards = []
for data in s_dict_cards:
	s_cards += [data[0]] * data[1]
	if(len(s_cards) >= N):
		break
# 出力
sum_val = sum(s_cards[:N])
print(sum_val)

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

# 昇順のカードとする
g_cards = list(map(int, input().split()))
cards = sorted(g_cards)

# 操作
for i in range(M):
	size, val = map(int, input().split())
	t_change = 0
	t_i = 0
	while((t_change < size) and (t_i < N)):
		# カード入れ替え
		if(cards[t_i] < val):
			cards[t_i] = val
			t_change += 1
		t_i += 1
	cards.sort()

print(sum(cards))

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

g_cards = list(map(int, input().split()))

# key-valueデータで管理
dict_cards = []
for card in g_cards:
	dict_cards.append([card, 1])

# 操作
for i in range(M):
	# sizeと同じ枚数のカードが増えたと考える
	size, val = map(int, input().split())
	dict_cards.append([val, size])

# 降順にする
s_dict_cards = sorted(dict_cards, key=lambda x: -x[0])

s_cards = []
for data in s_dict_cards:
	s_cards += [data[0]] * data[1]
	if(len(s_cards) >= N):
		break
# 出力
sum_val = sum(s_cards[:N])
print(sum_val)