Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import heapq

n, m = map(int, input().split())
a = map(int, input().split())

h = list()
for val in a:
    heapq.heappush(h, val)

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

print(sum(heapq.nlargest(n, h)))

Fixed submission:
# -*- coding: utf-8 -*-
from collections import defaultdict
n, m = map(int, input().split())
a = list(map(int, input().split()))

cards = defaultdict(int)
for num in a:
    cards[num] += 1

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

count = n
ans = 0
for key, val in sorted(cards.items(), key=lambda x: -x[0]):
    if count - val < 0:
        ans += key * count
        break
    else:
        ans += key * val
        count -= val
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))

for _ in range(m):
    b, c = map(int, input().split())
    a.extend([c for i in range(b)])
    a.sort(reverse=True)
    a = a[:n]

print(sum(a))

Fixed submission:
# -*- coding: utf-8 -*-
from collections import defaultdict
n, m = map(int, input().split())
a = list(map(int, input().split()))

cards = defaultdict(int)
for num in a:
    cards[num] += 1

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

count = n
ans = 0
for key, val in sorted(cards.items(), key=lambda x: -x[0]):
    if count - val < 0:
        ans += key * count
        break
    else:
        ans += key * val
        count -= val
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))

for _ in range(m):
    b, c = map(int, input().split())
    a.extend([c for i in range(b)])

a.sort(reverse=True)
print(sum(a[:n]))

Fixed submission:
# -*- coding: utf-8 -*-
from collections import defaultdict
n, m = map(int, input().split())
a = list(map(int, input().split()))

cards = defaultdict(int)
for num in a:
    cards[num] += 1

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

count = n
ans = 0
for key, val in sorted(cards.items(), key=lambda x: -x[0]):
    if count - val < 0:
        ans += key * count
        break
    else:
        ans += key * val
        count -= val
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

for _ in range(m):
    b, c = map(int, input().split())
    for i in range(b):
        a[i] = max(a[i], c)
    a.sort()
print(sum(a))

Fixed submission:
# -*- coding: utf-8 -*-
from collections import defaultdict
n, m = map(int, input().split())
a = list(map(int, input().split()))

cards = defaultdict(int)
for num in a:
    cards[num] += 1

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

count = n
ans = 0
for key, val in sorted(cards.items(), key=lambda x: -x[0]):
    if count - val < 0:
        ans += key * count
        break
    else:
        ans += key * val
        count -= val
print(ans)