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

n,m = map(int,input().split())
l = list(map(int,input().split()))

hq  = sorted(tuple(Counter(l).items()), key=lambda x:x[0])

heapq.heapify(hq)
# print(hq)

for i in range(m):
    b,c = map(int,input().split())
    while b > 0:
        x,y = heapq.heappop(hq)
        if x < c:
            if b < y:
                heapq.heappush(hq,(c,b))
                heapq.heappush(hq,(x,y-b))
                b = 0
            else:
                heapq.heappush(hq,(c,y))
                b -= y
        else:
            heapq.heappush(hq,(x,y))
            break
    # print(hq)

ans = 0
for i in range(len(hq)):
    ans += hq[i][0]*hq[i][1]
print(ans)

Fixed submission:
from collections import Counter
import heapq

n,m = map(int,input().split())
l = list(map(int,input().split()))

hq  = sorted(tuple(Counter(l).items()), key=lambda x:x[0])

heapq.heapify(hq)
# print(hq)
BC = [list(map(int, input().split())) for i in range(m)]
BC = sorted(BC,key = lambda x:-x[1])

for b,c in BC:
    while b > 0:
        x,y = heapq.heappop(hq)
        if x < c:
            if b < y:
                heapq.heappush(hq,(c,b))
                heapq.heappush(hq,(x,y-b))
                b = 0
            else:
                heapq.heappush(hq,(c,y))
                b -= y
        else:
            heapq.heappush(hq,(x,y))
            break
    # print(hq)

ans = 0
for i in range(len(hq)):
    ans += hq[i][0]*hq[i][1]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from collections import Counter
import heapq

n,m = map(int,input().split())
l = list(map(int,input().split()))

hq  = sorted(tuple(Counter(l).items()), key=lambda x:x[0])

heapq.heapify(hq)
# print(hq)

for i in range(m):
    b,c = map(int,input().split())
    while b > 0:
        x,y = heapq.heappop(hq)
        if x < c:
            if b < y:
                heapq.heappush(hq,(c,b))
                heapq.heappush(hq,(x,y-b))
                b = 0
            else:
                heapq.heappush(hq,(c,y))
                b -= y
        else:
            heapq.heappush(hq,(x,y))
            break
    # print(hq)

ans = 0
for i in range(len(hq)):
    ans += hq[i][0]*hq[i][1]
print(ans)

Fixed submission:
from collections import Counter
import heapq

n,m = map(int,input().split())
l = list(map(int,input().split()))

hq  = sorted(tuple(Counter(l).items()), key=lambda x:x[0])

heapq.heapify(hq)
# print(hq)
BC = [list(map(int, input().split())) for i in range(m)]
BC = sorted(BC,key = lambda x:-x[1])

for b,c in BC:
    while b > 0:
        x,y = heapq.heappop(hq)
        if x < c:
            if b < y:
                heapq.heappush(hq,(c,b))
                heapq.heappush(hq,(x,y-b))
                b = 0
            else:
                heapq.heappush(hq,(c,y))
                b -= y
        else:
            heapq.heappush(hq,(x,y))
            break
    # print(hq)

ans = 0
for i in range(len(hq)):
    ans += hq[i][0]*hq[i][1]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
import heapq

n,m = map(int,input().split())
l = list(map(int,input().split()))

heapq.heapify(l)

for i in range(m):
    b,c = map(int,input().split())
    cnt = 0
    while cnt < b:
        x = heapq.heappop(l)
        if x < c:
            x = c
        heapq.heappush(l,x)
        cnt += 1
print(sum(l))

Fixed submission:
from collections import Counter
import heapq

n,m = map(int,input().split())
l = list(map(int,input().split()))

hq  = sorted(tuple(Counter(l).items()), key=lambda x:x[0])

heapq.heapify(hq)
# print(hq)
BC = [list(map(int, input().split())) for i in range(m)]
BC = sorted(BC,key = lambda x:-x[1])

for b,c in BC:
    while b > 0:
        x,y = heapq.heappop(hq)
        if x < c:
            if b < y:
                heapq.heappush(hq,(c,b))
                heapq.heappush(hq,(x,y-b))
                b = 0
            else:
                heapq.heappush(hq,(c,y))
                b -= y
        else:
            heapq.heappush(hq,(x,y))
            break
    # print(hq)

ans = 0
for i in range(len(hq)):
    ans += hq[i][0]*hq[i][1]
print(ans)