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

h = []
h2 = []
N, M = [int(i) for i in input().split()]
count = dict()
for i in input().split():
    if not count.get(i):
        count[i] = 0
    count[i] += 1
for k, v in count.items():
    heapq.heappush(h, (int(k), v))

for _ in range(M):
    B, C = [int(i) for i in input().split()]
    heapq.heappush(h2, (C, B))

for i in range(M - 1, -1, -1):
    C, B = h2[i]
    while B > 0:
        if h[0][0] >= C:
            break
        m = h[0][0]
        mc = h[0][1]
        if mc > B:
            heapq.heappush(h, (C, B))
            h[0] = (m, mc - B)
        else:
            heapq.heappop(h)
            heapq.heappush(h, (C, mc))
        B -= mc
print(sum([i[0] * i[1] for i in h]))



Fixed submission:
import heapq

h = []
h2 = []
N, M = [int(i) for i in input().split()]
count = dict()
count2 = dict()
for i in input().split():
    if not count.get(i):
        count[i] = 0
    count[i] += 1
for k, v in count.items():
    heapq.heappush(h, (int(k), v))

for _ in range(M):
    B, C = [int(i) for i in input().split()]
    if not count2.get(C):
        count2[C] = 0
    count2[C] += B
for k, v in count2.items():
    heapq.heappush(h2, (-k, v))
while len(h2) > 0:
    C, B = heapq.heappop(h2)
    C = -C
    while B > 0:
        if h[0][0] >= C:
            break
        m = h[0][0]
        mc = h[0][1]
        if mc > B:
            heapq.heappush(h, (C, B))
            h[0] = (m, mc - B)
        else:
            heapq.heappop(h)
            heapq.heappush(h, (C, mc))
        B -= mc
print(sum([i[0] * i[1] for i in h]))

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

h = []
N, M = [int(i) for i in input().split()]
count = dict()
for i in input().split():
    if not count.get(i):
        count[i] = 0
    count[i] += 1
for k, v in count.items():
    heapq.heappush(h, (int(k), v))

for _ in range(M):
    B, C = [int(i) for i in input().split()]
    while B > 0:
        if h[0][0] >= C:
            break
        m = heapq.heappop(h)
        if m[1] > B:
            heapq.heappush(h, (C, B))
            heapq.heappush(h, (m[0], m[1] - B))
        else:
            heapq.heappush(h, (C, m[1]))
        B -= m[1]
print(sum([i[0] * i[1] for i in h]))



Fixed submission:
import heapq

h = []
h2 = []
N, M = [int(i) for i in input().split()]
count = dict()
count2 = dict()
for i in input().split():
    if not count.get(i):
        count[i] = 0
    count[i] += 1
for k, v in count.items():
    heapq.heappush(h, (int(k), v))

for _ in range(M):
    B, C = [int(i) for i in input().split()]
    if not count2.get(C):
        count2[C] = 0
    count2[C] += B
for k, v in count2.items():
    heapq.heappush(h2, (-k, v))
while len(h2) > 0:
    C, B = heapq.heappop(h2)
    C = -C
    while B > 0:
        if h[0][0] >= C:
            break
        m = h[0][0]
        mc = h[0][1]
        if mc > B:
            heapq.heappush(h, (C, B))
            h[0] = (m, mc - B)
        else:
            heapq.heappop(h)
            heapq.heappush(h, (C, mc))
        B -= mc
print(sum([i[0] * i[1] for i in h]))

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

h = []
N, M = [int(i) for i in input().split()]
for i in input().split():
    heapq.heappush(h, int(i))

for _ in range(M):
    B, C = [int(i) for i in input().split()]
    for j in range(B):
        if h[0] >= C:
            break
        heapq.heappop(h)
        heapq.heappush(h, C)

print(sum(h))



Fixed submission:
import heapq

h = []
h2 = []
N, M = [int(i) for i in input().split()]
count = dict()
count2 = dict()
for i in input().split():
    if not count.get(i):
        count[i] = 0
    count[i] += 1
for k, v in count.items():
    heapq.heappush(h, (int(k), v))

for _ in range(M):
    B, C = [int(i) for i in input().split()]
    if not count2.get(C):
        count2[C] = 0
    count2[C] += B
for k, v in count2.items():
    heapq.heappush(h2, (-k, v))
while len(h2) > 0:
    C, B = heapq.heappop(h2)
    C = -C
    while B > 0:
        if h[0][0] >= C:
            break
        m = h[0][0]
        mc = h[0][1]
        if mc > B:
            heapq.heappush(h, (C, B))
            h[0] = (m, mc - B)
        else:
            heapq.heappop(h)
            heapq.heappush(h, (C, mc))
        B -= mc
print(sum([i[0] * i[1] for i in h]))