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


n, m = map(int, input().split())
al = map(int, input().split())
h = []
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1
    

for i in range(m):
    b, c = map(int, input().split())
    
    if c not in ad:
        ad[c] = b
    else:
        ad[c] += b


keys = list( ad.keys() )
keys.sort(reverse=True)



for k in keys:
    for i in range(ad[k]):
        heapq.heappush(h, k)

    if len(h) >= n:
        break


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

Fixed submission:
import heapq


n, m = map(int, input().split())
al = map(int, input().split())
h = []
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1
    

for i in range(m):
    b, c = map(int, input().split())
    
    if c not in ad:
        ad[c] = b
    else:
        ad[c] += b


keys = list( ad.keys() )
keys.sort(reverse=True)



for k in keys:
    for i in range(ad[k]):
        heapq.heappush(h, k)

        if len(h) >= n:
            break


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

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

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

for a in al:
    heapq.heappush(h, a)

for i in range(m):
    b, c = map(int, input().split())
    if len(h) >= n and c <= heapq.nlargest(n, h)[-1]:
        continue

    if b > n:
        b = n
    for j in range(b):
        heapq.heappush(h, c)




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

Fixed submission:
import heapq


n, m = map(int, input().split())
al = map(int, input().split())
h = []
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1
    

for i in range(m):
    b, c = map(int, input().split())
    
    if c not in ad:
        ad[c] = b
    else:
        ad[c] += b


keys = list( ad.keys() )
keys.sort(reverse=True)



for k in keys:
    for i in range(ad[k]):
        heapq.heappush(h, k)

        if len(h) >= n:
            break


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

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

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

for a in al:
    heapq.heappush(h, a)

for i in range(m):
    b, c = map(int, input().split())
    if len(h) >= n and c <= heapq.nlargest(n, h)[-1]:
        continue

    for j in range(b):
        heapq.heappush(h, c)




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

Fixed submission:
import heapq


n, m = map(int, input().split())
al = map(int, input().split())
h = []
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1
    

for i in range(m):
    b, c = map(int, input().split())
    
    if c not in ad:
        ad[c] = b
    else:
        ad[c] += b


keys = list( ad.keys() )
keys.sort(reverse=True)



for k in keys:
    for i in range(ad[k]):
        heapq.heappush(h, k)

        if len(h) >= n:
            break


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

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

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

for a in al:
    heapq.heappush(h, a)

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


[heapq.heappop(h) for i in range(n)]

sum = 0
for i in range(n):
    sum += heapq.heappop(h)

print(sum)

Fixed submission:
import heapq


n, m = map(int, input().split())
al = map(int, input().split())
h = []
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1
    

for i in range(m):
    b, c = map(int, input().split())
    
    if c not in ad:
        ad[c] = b
    else:
        ad[c] += b


keys = list( ad.keys() )
keys.sort(reverse=True)



for k in keys:
    for i in range(ad[k]):
        heapq.heappush(h, k)

        if len(h) >= n:
            break


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

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
al = map(int, input().split())
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1


keys = list(ad.keys())
keys.sort()
for i in range(m):
    b, c = map(int, input().split())
    
    if c == 1:
        break
    
    changed = False
    for a in keys:

        if a >= c:
            break
        
        k = 0
        if b > ad[a]:
            k = ad[a]
            ad[a] = 0
            b -= k
        else:
            ad[a] -= b
            k = b
            b = 0
        
        if c not in ad:
            ad[c] = k
            changed = True
        else:
            ad[c] += k
        

    if changed:
        keys = list(ad.keys())
        keys.sort()


sum = 0
for a in ad.keys():
    sum += a * ad[a]

print(sum)

Fixed submission:
import heapq


n, m = map(int, input().split())
al = map(int, input().split())
h = []
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1
    

for i in range(m):
    b, c = map(int, input().split())
    
    if c not in ad:
        ad[c] = b
    else:
        ad[c] += b


keys = list( ad.keys() )
keys.sort(reverse=True)



for k in keys:
    for i in range(ad[k]):
        heapq.heappush(h, k)

        if len(h) >= n:
            break


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

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
al = map(int, input().split())
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1


keys = list(ad.keys())
keys.sort()
for i in range(m):
    b, c = map(int, input().split())
    
    if c == 1:
        break
    
    changed = False
    for a in keys:

        if a >= c:
            break
        
        k = 0
        if b > ad[a]:
            k = ad[a]
            ad[a] = 0
            b -= k
        else:
            ad[a] -= b
            k = b
            b = 0
        
        if c not in ad:
            ad[c] = k
        else:
            ad[c] += k
        
        changed = True

    if changed:
        keys = list(ad.keys())
        keys.sort()


sum = 0
for a in ad.keys():
    sum += a * ad[a]

print(sum)

Fixed submission:
import heapq


n, m = map(int, input().split())
al = map(int, input().split())
h = []
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1
    

for i in range(m):
    b, c = map(int, input().split())
    
    if c not in ad:
        ad[c] = b
    else:
        ad[c] += b


keys = list( ad.keys() )
keys.sort(reverse=True)



for k in keys:
    for i in range(ad[k]):
        heapq.heappush(h, k)

        if len(h) >= n:
            break


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

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
al = map(int, input().split())
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1


keys = list(ad.keys())
keys.sort()
for i in range(m):
    b, c = map(int, input().split())
    
    if c == 1:
        break
    
    for a in keys:

        if a >= c:
            break
        
        k = 0
        if b > ad[a]:
            k = ad[a]
            ad[a] = 0
            b -= k
        else:
            ad[a] -= b
            k = b
            b = 0
        
        if c not in ad:
            ad[c] = k
        else:
            ad[c] += k
        
        keys = list(ad.keys())
        keys.sort()


sum = 0
for a in ad.keys():
    sum += a * ad[a]

print(sum)

Fixed submission:
import heapq


n, m = map(int, input().split())
al = map(int, input().split())
h = []
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1
    

for i in range(m):
    b, c = map(int, input().split())
    
    if c not in ad:
        ad[c] = b
    else:
        ad[c] += b


keys = list( ad.keys() )
keys.sort(reverse=True)



for k in keys:
    for i in range(ad[k]):
        heapq.heappush(h, k)

        if len(h) >= n:
            break


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

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
al = map(int, input().split())
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1


for i in range(m):
    b, c = map(int, input().split())
    
    keys = list(ad.keys())
    keys.sort()
    for a in keys:

        if a >= c:
            break
        
        k = 0
        if b > ad[a]:
            k = ad[a]
            ad[a] = 0
            b -= k
        else:
            ad[a] -= b
            k = b
            b = 0
        
        if c not in ad:
            ad[c] = k
        else:
            ad[c] += k


sum = 0
for a in ad.keys():
    sum += a * ad[a]

print(sum)

Fixed submission:
import heapq


n, m = map(int, input().split())
al = map(int, input().split())
h = []
ad = {}
for a in al:
    if a not in ad:
        ad[a] = 1
    else:
        ad[a] += 1
    

for i in range(m):
    b, c = map(int, input().split())
    
    if c not in ad:
        ad[c] = b
    else:
        ad[c] += b


keys = list( ad.keys() )
keys.sort(reverse=True)



for k in keys:
    for i in range(ad[k]):
        heapq.heappush(h, k)

        if len(h) >= n:
            break


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