Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N, M = map(int, input().split())
As = sorted(list(map(int, input().split())), reverse=True)
cards = np.array(As, dtype=np.int64) * -1

for i in range(M):
    B, C = map(int, input().split())

    p = np.searchsorted(cards, -C)
    if p >= N:
        continue

    replace = min(N - p, B)
    cards[p+replace:] = cards[p:N-replace]
    cards[p:p+replace] = -C

total = 0
for card in cards:
    total += -card
print(total)

Fixed submission:
import heapq
from collections import defaultdict

N, M = map(int, input().split())

As = map(int, input().split())
counts = defaultdict(int)
for A in As:
    counts[A] += 1
h = sorted(counts.keys())

for i in range(M):
    B, C = map(int, input().split())

    if C not in counts:
        heapq.heappush(h, C)
    counts[C] += B
    if len(h) > N:
        v = heapq.heappop(h)
        del counts[v]

total_count = 0
total = 0
for k in sorted(h, reverse=True):
    c = counts[k]
    c = min(c, N - total_count)
    total += k * c
    total_count += c
    if total_count > N:
        break
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import bisect
N, M = map(int, input().split())
As = sorted(list(map(int, input().split())), reverse=True)
cards = np.array(As, dtype=np.int64) * -1

for i in range(M):
    B, C = map(int, input().split())

    p = bisect.bisect_right(cards, -C)
    if p >= N:
        continue

    replace = min(N - p, B)
    cards[p+replace:] = cards[p:N-replace]
    cards[p:p+replace] = -C

total = 0
for card in cards:
    total += -card
print(total)

Fixed submission:
import heapq
from collections import defaultdict

N, M = map(int, input().split())

As = map(int, input().split())
counts = defaultdict(int)
for A in As:
    counts[A] += 1
h = sorted(counts.keys())

for i in range(M):
    B, C = map(int, input().split())

    if C not in counts:
        heapq.heappush(h, C)
    counts[C] += B
    if len(h) > N:
        v = heapq.heappop(h)
        del counts[v]

total_count = 0
total = 0
for k in sorted(h, reverse=True):
    c = counts[k]
    c = min(c, N - total_count)
    total += k * c
    total_count += c
    if total_count > N:
        break
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import bisect
N, M = map(int, input().split())
As = sorted(list(map(int, input().split())), reverse=True)
cards = np.array(As, dtype=np.int64) * -1

for i in range(M):
    B, C = map(int, input().split())

    p = bisect.bisect_right(cards, -C)
    if p >= N:
        continue

    replace = min(N - p, B)
    cards[p+replace:] = cards[p:N-replace]
    new_cards = np.array([-C]*replace, dtype=np.int64)
    cards[p:p+replace] = new_cards

total = 0
for card in cards:
    total += -card
print(total)

Fixed submission:
import heapq
from collections import defaultdict

N, M = map(int, input().split())

As = map(int, input().split())
counts = defaultdict(int)
for A in As:
    counts[A] += 1
h = sorted(counts.keys())

for i in range(M):
    B, C = map(int, input().split())

    if C not in counts:
        heapq.heappush(h, C)
    counts[C] += B
    if len(h) > N:
        v = heapq.heappop(h)
        del counts[v]

total_count = 0
total = 0
for k in sorted(h, reverse=True):
    c = counts[k]
    c = min(c, N - total_count)
    total += k * c
    total_count += c
    if total_count > N:
        break
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import bisect
N, M = map(int, input().split())
As = sorted(list(map(int, input().split())), reverse=True)
cards = np.array(As, dtype=np.int64) * -1

for i in range(M):
    B, C = map(int, input().split())

    p = bisect.bisect_right(cards, -C)
    if p >= N:
        continue

    replace = min(N - p, B)
    new_cards = np.array([-C]*replace, dtype=np.int64)
    cards = np.concatenate([cards[:p], new_cards,  cards[p:N-replace]])

total = 0
for card in cards:
    total += -card
print(total)

Fixed submission:
import heapq
from collections import defaultdict

N, M = map(int, input().split())

As = map(int, input().split())
counts = defaultdict(int)
for A in As:
    counts[A] += 1
h = sorted(counts.keys())

for i in range(M):
    B, C = map(int, input().split())

    if C not in counts:
        heapq.heappush(h, C)
    counts[C] += B
    if len(h) > N:
        v = heapq.heappop(h)
        del counts[v]

total_count = 0
total = 0
for k in sorted(h, reverse=True):
    c = counts[k]
    c = min(c, N - total_count)
    total += k * c
    total_count += c
    if total_count > N:
        break
print(total)

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

cards = sorted(list(map(int, input().split())), reverse=True)

for i in range(M):
    B, C = map(int, input().split())
    cards.extend([C]*B)
    cards = sorted(cards, reverse=True)
    cards = cards[:N]

total = 0
for card in cards:
    total += card
print(total)

Fixed submission:
import heapq
from collections import defaultdict

N, M = map(int, input().split())

As = map(int, input().split())
counts = defaultdict(int)
for A in As:
    counts[A] += 1
h = sorted(counts.keys())

for i in range(M):
    B, C = map(int, input().split())

    if C not in counts:
        heapq.heappush(h, C)
    counts[C] += B
    if len(h) > N:
        v = heapq.heappop(h)
        del counts[v]

total_count = 0
total = 0
for k in sorted(h, reverse=True):
    c = counts[k]
    c = min(c, N - total_count)
    total += k * c
    total_count += c
    if total_count > N:
        break
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

N, M = map(int, input().split())

counts = defaultdict(int)
As = map(int, input().split())
for A in As:
    counts[A] += 1

for i in range(M):
    B, C = map(int, input().split())
    keys = sorted(counts.keys())
    key_index = 0
    while B > 0:
        if C <= keys[key_index]:
            break
        replace = min(B, counts[keys[key_index]])
        B -= replace
        counts[keys[key_index]] -= replace
        if counts[keys[key_index]] == 0:
            del counts[keys[key_index]]
        counts[C] += replace
        key_index += 1

total = 0
for n, c in counts.items():
    total += n * c
print(total)

Fixed submission:
import heapq
from collections import defaultdict

N, M = map(int, input().split())

As = map(int, input().split())
counts = defaultdict(int)
for A in As:
    counts[A] += 1
h = sorted(counts.keys())

for i in range(M):
    B, C = map(int, input().split())

    if C not in counts:
        heapq.heappush(h, C)
    counts[C] += B
    if len(h) > N:
        v = heapq.heappop(h)
        del counts[v]

total_count = 0
total = 0
for k in sorted(h, reverse=True):
    c = counts[k]
    c = min(c, N - total_count)
    total += k * c
    total_count += c
    if total_count > N:
        break
print(total)