Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
cards = list(map(int,input().split()))

import heapq

heapq.heapify(cards)

for _ in range(m):
    b,c = map(int,input().split())

    for _ in range(b):
        heapq.heappush(cards,c)

for _ in range(len(cards) - n):
    heapq.heappop(cards)

print(sum(cards))

Fixed submission:
n,m = map(int,input().split())
cards = list(map(int,input().split()))

from collections import Counter

d = dict(Counter(cards))

for _ in range(m):
    b,c = map(int,input().split())

    if c in d:
        d[c]+=b
    else:
        d[c]=b

d= sorted(d.items(),key=lambda x: x[0],reverse=True)

ans = 0
cnt = 0
for j in d:
    cnt +=j[1]
    ans +=j[0] * j[1]



    if cnt>n:
        ans -=j[0]*(cnt-n)
        print(ans)
        exit(0)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
cards = list(map(int,input().split()))

from collections import deque

cards = deque(cards)

for _ in range(m):
    b,c = map(int,input().split())

    cards+=[c]*b

cards = sorted(cards,reverse=True)

total = 0
for i in range(n):
    total += cards[i]

print(total)


Fixed submission:
n,m = map(int,input().split())
cards = list(map(int,input().split()))

from collections import Counter

d = dict(Counter(cards))

for _ in range(m):
    b,c = map(int,input().split())

    if c in d:
        d[c]+=b
    else:
        d[c]=b

d= sorted(d.items(),key=lambda x: x[0],reverse=True)

ans = 0
cnt = 0
for j in d:
    cnt +=j[1]
    ans +=j[0] * j[1]



    if cnt>n:
        ans -=j[0]*(cnt-n)
        print(ans)
        exit(0)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
cards = list(map(int,input().split()))

from collections import deque

cards = deque(cards)

for _ in range(m):
    b,c = map(int,input().split())

    for _ in range(b):
        cards.append(c)


cards = sorted(cards,reverse=True)

total = 0
for i in range(n):
    total += cards[i]

print(total)


Fixed submission:
n,m = map(int,input().split())
cards = list(map(int,input().split()))

from collections import Counter

d = dict(Counter(cards))

for _ in range(m):
    b,c = map(int,input().split())

    if c in d:
        d[c]+=b
    else:
        d[c]=b

d= sorted(d.items(),key=lambda x: x[0],reverse=True)

ans = 0
cnt = 0
for j in d:
    cnt +=j[1]
    ans +=j[0] * j[1]



    if cnt>n:
        ans -=j[0]*(cnt-n)
        print(ans)
        exit(0)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
cards = list(map(int,input().split()))

from collections import deque
import bisect

cards = deque(sorted(cards))

for _ in range(m):
    b,c = map(int,input().split())

    for _ in range(b):
        cards.append(c)


cards = sorted(cards,reverse=True)[:n]

print(sum(cards))


Fixed submission:
n,m = map(int,input().split())
cards = list(map(int,input().split()))

from collections import Counter

d = dict(Counter(cards))

for _ in range(m):
    b,c = map(int,input().split())

    if c in d:
        d[c]+=b
    else:
        d[c]=b

d= sorted(d.items(),key=lambda x: x[0],reverse=True)

ans = 0
cnt = 0
for j in d:
    cnt +=j[1]
    ans +=j[0] * j[1]



    if cnt>n:
        ans -=j[0]*(cnt-n)
        print(ans)
        exit(0)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
cards = list(map(int,input().split()))

from collections import deque
import bisect

cards = sorted(cards)

for _ in range(m):
    b,c = map(int,input().split())

    index = bisect.bisect_left(cards,c)

    for i in range(index):
        if i+1>b:
            continue
        cards.pop(0)
        cards.insert(index-1,c)


print(sum(cards))


Fixed submission:
n,m = map(int,input().split())
cards = list(map(int,input().split()))

from collections import Counter

d = dict(Counter(cards))

for _ in range(m):
    b,c = map(int,input().split())

    if c in d:
        d[c]+=b
    else:
        d[c]=b

d= sorted(d.items(),key=lambda x: x[0],reverse=True)

ans = 0
cnt = 0
for j in d:
    cnt +=j[1]
    ans +=j[0] * j[1]



    if cnt>n:
        ans -=j[0]*(cnt-n)
        print(ans)
        exit(0)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
cards = list(map(int,input().split()))
cards = sorted(cards)


import bisect

for _ in range(m):
    b,c = map(int,input().split())

    index = bisect.bisect_left(cards,c)

    for i in range(index):
        if i+1>b:
            continue
        cards[i]=c

    cards=sorted(cards)
    
print(sum(cards))


Fixed submission:
n,m = map(int,input().split())
cards = list(map(int,input().split()))

from collections import Counter

d = dict(Counter(cards))

for _ in range(m):
    b,c = map(int,input().split())

    if c in d:
        d[c]+=b
    else:
        d[c]=b

d= sorted(d.items(),key=lambda x: x[0],reverse=True)

ans = 0
cnt = 0
for j in d:
    cnt +=j[1]
    ans +=j[0] * j[1]



    if cnt>n:
        ans -=j[0]*(cnt-n)
        print(ans)
        exit(0)

print(ans)