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

n,m = map(int,input().split())
a = list(map(int,input().split()))
heapq.heapify(a)
tmp = 0
for i in range(m):
    b,c = map(int,input().split())
    for j in range(b):
        if c >= tmp:
            p = heapq.heappop(a)
            if c > p:
                heapq.heappush(a,c)
            else:
                heapq.heappush(a,p)
                tmp = p
                break
            
        tmp = p
            
print(sum(a))

Fixed submission:
#D
import heapq

n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = []
for i in range(m):
    b,c = map(int,input().split())
    bc.append((b,c))
bc.sort(key=lambda x:-x[1])
heapq.heapify(a)
for i in range(m):
    b,c = bc[i][0],bc[i][1]
    for j in range(b):
        p = heapq.heappop(a)
        if c > p:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,p)
            break
            
            
print(sum(a))

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

n,m = map(int,input().split())
a = list(map(int,input().split()))
heapq.heapify(a)
tmp = 0
for i in range(m):
    b,c = map(int,input().split())
    for j in range(b):
        if c >= tmp
            p = heapq.heappop(a)
            if c > p:
                heapq.heappush(a,c)
            else:
                heapq.heappush(a,p)
                tmp = p
                break
            
        tmp = p
            
print(sum(a))

Fixed submission:
#D
import heapq

n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = []
for i in range(m):
    b,c = map(int,input().split())
    bc.append((b,c))
bc.sort(key=lambda x:-x[1])
heapq.heapify(a)
for i in range(m):
    b,c = bc[i][0],bc[i][1]
    for j in range(b):
        p = heapq.heappop(a)
        if c > p:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,p)
            break
            
            
print(sum(a))

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

n,m = map(int,input().split())
a = list(map(int,input().split()))
heapq.heapify(a)
tmp = 0
for i in range(m):
    b,c = map(int,input().split())
    for j in range(b):
        if c >= tmp
            p = heapq.heappop(a)
            if c > p:
                heapq.heappush(a,c)
            else:
                heapq.heappush(a,p)
                tmp = p
                break
            
            tmp = p
            
print(sum(a))

Fixed submission:
#D
import heapq

n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = []
for i in range(m):
    b,c = map(int,input().split())
    bc.append((b,c))
bc.sort(key=lambda x:-x[1])
heapq.heapify(a)
for i in range(m):
    b,c = bc[i][0],bc[i][1]
    for j in range(b):
        p = heapq.heappop(a)
        if c > p:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,p)
            break
            
            
print(sum(a))

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

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

for i in range(m):
    b,c = map(int,input().split())
    for j in range(b):
        p = heapq.heappop(a)
        if c > p:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,p)
            break
            
print(sum(a))

Fixed submission:
#D
import heapq

n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = []
for i in range(m):
    b,c = map(int,input().split())
    bc.append((b,c))
bc.sort(key=lambda x:-x[1])
heapq.heapify(a)
for i in range(m):
    b,c = bc[i][0],bc[i][1]
    for j in range(b):
        p = heapq.heappop(a)
        if c > p:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,p)
            break
            
            
print(sum(a))

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

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

for i in range(m):
    b,c = map(int,input().split())
    for j in range(b):
        p = heapq.heappop(a)
        if c > p:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,p)
            break
            
print(sum(a))

Fixed submission:
#D
import heapq

n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = []
for i in range(m):
    b,c = map(int,input().split())
    bc.append((b,c))
bc.sort(key=lambda x:-x[1])
heapq.heapify(a)
for i in range(m):
    b,c = bc[i][0],bc[i][1]
    for j in range(b):
        p = heapq.heappop(a)
        if c > p:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,p)
            break
            
            
print(sum(a))