Buggy python submission (verdict: Time Limit Exceeded):
import bisect

card_count, rewrite_count = map(int, input().split(" "))
cards = list(map(int, input().split(" ")))

cards.sort()

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    insert_point = bisect.bisect_left(cards, rewrite_number)

    cards = cards[:insert_point] + ([rewrite_number] * stock_count) + cards[insert_point:]
    # for _ in range(stock_count):
    #     cards.insert(insert_point, rewrite_number)
    
    del cards[:stock_count]

print(sum(cards))


Fixed submission:
import bisect

card_count, rewrite_count = map(int, input().split(" "))

cards = {}
for card in list(map(int, input().split(" "))):
    count = cards.get(card, 0)
    cards[card] = count + 1

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    count = cards.get(rewrite_number, 0)
    cards[rewrite_number] = count + stock_count

number_list = list(cards.keys())
number_list.sort(reverse=True)

total = 0
for number in number_list:
    if cards[number] < card_count:
        total += number * cards[number]
    else:
        total += number * card_count
        break
    card_count -= cards[number]

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

card_count, rewrite_count = map(int, input().split(" "))
cards = list(map(int, input().split(" ")))

cards.sort()

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    insert_point = bisect.bisect_left(cards, rewrite_number)

    for _ in range(stock_count):
        cards.insert(insert_point, rewrite_number)
    
    del cards[:stock_count]

print(sum(cards))


Fixed submission:
import bisect

card_count, rewrite_count = map(int, input().split(" "))

cards = {}
for card in list(map(int, input().split(" "))):
    count = cards.get(card, 0)
    cards[card] = count + 1

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    count = cards.get(rewrite_number, 0)
    cards[rewrite_number] = count + stock_count

number_list = list(cards.keys())
number_list.sort(reverse=True)

total = 0
for number in number_list:
    if cards[number] < card_count:
        total += number * cards[number]
    else:
        total += number * card_count
        break
    card_count -= cards[number]

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
card_count, rewrite_count = map(int, input().split(" "))
cards = list(map(int, input().split(" ")))

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    cards.extend((rewrite_number,) * stock_count)

cards.sort()

print(sum(cards[-card_count:]))


Fixed submission:
import bisect

card_count, rewrite_count = map(int, input().split(" "))

cards = {}
for card in list(map(int, input().split(" "))):
    count = cards.get(card, 0)
    cards[card] = count + 1

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    count = cards.get(rewrite_number, 0)
    cards[rewrite_number] = count + stock_count

number_list = list(cards.keys())
number_list.sort(reverse=True)

total = 0
for number in number_list:
    if cards[number] < card_count:
        total += number * cards[number]
    else:
        total += number * card_count
        break
    card_count -= cards[number]

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
card_count, rewrite_count = map(int, input().split(" "))
cards = list(map(int, input().split(" ")))

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    for _ in range(stock_count):
        minimum_index = cards.index(min(cards))

        if rewrite_number <= cards[minimum_index]:
            break

        cards[minimum_index] = rewrite_number

print(sum(cards))


Fixed submission:
import bisect

card_count, rewrite_count = map(int, input().split(" "))

cards = {}
for card in list(map(int, input().split(" "))):
    count = cards.get(card, 0)
    cards[card] = count + 1

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    count = cards.get(rewrite_number, 0)
    cards[rewrite_number] = count + stock_count

number_list = list(cards.keys())
number_list.sort(reverse=True)

total = 0
for number in number_list:
    if cards[number] < card_count:
        total += number * cards[number]
    else:
        total += number * card_count
        break
    card_count -= cards[number]

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
card_count, rewrite_count = map(int, input().split(" "))
cards = list(map(int, input().split(" ")))
cards.sort()

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    for _ in range(stock_count):
        minimum_index = cards.index(min(cards))

        if rewrite_number <= cards[minimum_index]:
            break

        cards[minimum_index] = rewrite_number

print(sum(cards))


Fixed submission:
import bisect

card_count, rewrite_count = map(int, input().split(" "))

cards = {}
for card in list(map(int, input().split(" "))):
    count = cards.get(card, 0)
    cards[card] = count + 1

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    count = cards.get(rewrite_number, 0)
    cards[rewrite_number] = count + stock_count

number_list = list(cards.keys())
number_list.sort(reverse=True)

total = 0
for number in number_list:
    if cards[number] < card_count:
        total += number * cards[number]
    else:
        total += number * card_count
        break
    card_count -= cards[number]

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
card_count, rewrite_count = map(int, input().split(" "))
cards = list(map(int, input().split(" ")))
cards.sort()

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    for index in range(stock_count):
        if rewrite_number <= cards[index]:
            break
        cards[index] = rewrite_number
    cards.sort()

print(sum(cards))

Fixed submission:
import bisect

card_count, rewrite_count = map(int, input().split(" "))

cards = {}
for card in list(map(int, input().split(" "))):
    count = cards.get(card, 0)
    cards[card] = count + 1

for _ in range(rewrite_count):
    stock_count, rewrite_number = map(int, input().split(" "))
    count = cards.get(rewrite_number, 0)
    cards[rewrite_number] = count + stock_count

number_list = list(cards.keys())
number_list.sort(reverse=True)

total = 0
for number in number_list:
    if cards[number] < card_count:
        total += number * cards[number]
    else:
        total += number * card_count
        break
    card_count -= cards[number]

print(total)