Buggy python submission (verdict: Runtime Error):
import bisect

n,m = map(int,input().split())
l = list(map(int,input().split()))
b_c = [list(map(int,input().split())) for i in range(m)]
b_c.sort(key=lambda x: x[1],reverse=True)
c_l = []
l.sort()

for i in range(m):
    c_l.extend([b_c[i][1]]*b_c[i][0])


idx = 0
for i in range(len(c_l)):
    if i == len(c_l)-1:
        idx = n-1
        break
    
    if c_l[i] < l[i]:
        idx = i
        break
l.sort(reverse=True)
print(sum(c_l[:idx])+sum(l[:n-idx]))

Fixed submission:
from heapq import heappush, heappop

n,m = map(int,input().split())
heap = []

for i in map(int,input().split()):
    heappush(heap,i)

bc = [list(map(int, input().split())) for _ in range(m)]
bc.sort(key = lambda x: -x[1])

for b,c in bc:
    for j in range(b):
        a = heappop(heap)
        if a < c:
            heappush(heap,c)
        
        else:
            heappush(heap, a)
            break
        
print(sum(heap))

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

n,m = map(int,input().split())
l = list(map(int,input().split()))
b_c = [list(map(int,input().split())) for i in range(m)]

l.sort()
b_c.sort(key=lambda x:x[1],reverse=True)

idx = 0
ans = 0

for i in range(m):
    if idx == n:
        break
    if l[idx:][0] >= b_c[i][1]:
        break
    
    temp = bisect.bisect_left(l[idx:],b_c[i][1])
    if temp > b_c[i][0]:
        temp = b_c[i][0]
    if temp > (n-idx-1):
        temp = n-idx
    ans += temp*b_c[i][1]
    idx += temp

ans += sum(l[idx:])
print(ans)

Fixed submission:
from heapq import heappush, heappop

n,m = map(int,input().split())
heap = []

for i in map(int,input().split()):
    heappush(heap,i)

bc = [list(map(int, input().split())) for _ in range(m)]
bc.sort(key = lambda x: -x[1])

for b,c in bc:
    for j in range(b):
        a = heappop(heap)
        if a < c:
            heappush(heap,c)
        
        else:
            heappush(heap, a)
            break
        
print(sum(heap))

Buggy python submission (verdict: Runtime Error):
import bisect

n,m = map(int,input().split())
l = list(map(int,input().split()))
b_c = [list(map(int,input().split())) for i in range(m)]

l.sort()
b_c.sort(key=lambda x:x[1],reverse=True)

idx = 0
ans = 0

for i in range(m):
    if l[idx:][0] >= b_c[i][1]:
        break
    
    temp = bisect.bisect_left(l[idx:],b_c[i][1])
    if temp > b_c[i][0]:
        temp = b_c[i][0]
    ans += temp*b_c[i][1]
    idx += temp

ans += sum(l[idx:])
print(ans)

Fixed submission:
from heapq import heappush, heappop

n,m = map(int,input().split())
heap = []

for i in map(int,input().split()):
    heappush(heap,i)

bc = [list(map(int, input().split())) for _ in range(m)]
bc.sort(key = lambda x: -x[1])

for b,c in bc:
    for j in range(b):
        a = heappop(heap)
        if a < c:
            heappush(heap,c)
        
        else:
            heappush(heap, a)
            break
        
print(sum(heap))

Buggy python submission (verdict: Runtime Error):
import bisect

n,m = map(int,input().split())
l = list(map(int,input().split()))
b_c = [list(map(int,input().split())) for i in range(m)]

l.sort()
b_c.sort(key=lambda x:x[1],reverse=True)

idx = 0
ans = 0

for i in range(m):
    if l[idx:][0] >= b_c[i][1]:
        break
    
    temp = bisect.bisect_left(l[idx:],b_c[i][1])
    if temp > b_c[i][0]:
        temp = b_c[i][0]
    ans += temp*b_c[i][1]
    idx += temp

ans += sum(l[idx:])
print(ans)

Fixed submission:
from heapq import heappush, heappop

n,m = map(int,input().split())
heap = []

for i in map(int,input().split()):
    heappush(heap,i)

bc = [list(map(int, input().split())) for _ in range(m)]
bc.sort(key = lambda x: -x[1])

for b,c in bc:
    for j in range(b):
        a = heappop(heap)
        if a < c:
            heappush(heap,c)
        
        else:
            heappush(heap, a)
            break
        
print(sum(heap))

Buggy python submission (verdict: Runtime Error):
import bisect

n,m = map(int,input().split())
l = list(map(int,input().split()))
b_c = [list(map(int,input().split())) for i in range(m)]

l.sort()
b_c.sort(key=lambda x:x[1],reverse=True)

print(b_c)

idx = 0
ans = 0

for i in range(m):
    if l[idx:][0] >= b_c[i][1]:
        break
    
    temp = bisect.bisect_left(l[idx:],b_c[i][1])
    if temp > b_c[i][0]:
        temp = b_c[i][0]
    ans += temp*b_c[i][1]
    idx += temp

ans += sum(l[idx:])
print(ans)

Fixed submission:
from heapq import heappush, heappop

n,m = map(int,input().split())
heap = []

for i in map(int,input().split()):
    heappush(heap,i)

bc = [list(map(int, input().split())) for _ in range(m)]
bc.sort(key = lambda x: -x[1])

for b,c in bc:
    for j in range(b):
        a = heappop(heap)
        if a < c:
            heappush(heap,c)
        
        else:
            heappush(heap, a)
            break
        
print(sum(heap))