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

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]
bc.sort(key=lambda x: x[1])
heapq.heapify(a)

for i in range(m):
  for j in range(bc[i][0]):
    temp = heapq.heappop(a)
    temp2 = bc[i][1]
    if temp2 > temp:
      heapq.heappush(a, temp2)
    else:
      heapq.heappush(a, temp)
      break
      
print(sum(a))

Fixed submission:
import heapq
 
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]
bc.sort(key=lambda x: x[1], reverse = True)
heapq.heapify(a)
 
for i in range(m):
  for j in range(bc[i][0]):
    temp = heapq.heappop(a)
    temp2 = bc[i][1]
    if temp2 > temp:
      heapq.heappush(a, temp2)
    else:
      heapq.heappush(a, temp)
      print(sum(a))
      quit()
      
print(sum(a))

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

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]
bc.sort(key=lambda x: x[1])
heapq.heapify(a)

for i in range(m):
  for j in range(bc[i][0]):
    temp = heapq.heappop(a)
    temp2 = bc[i][1]
    if temp2 > temp:
      heapq.heappush(a, temp2)
    else:
      heapq.heappush(a, temp)
      break
      
print(sum(a))

Fixed submission:
import heapq
 
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]
bc.sort(key=lambda x: x[1], reverse = True)
heapq.heapify(a)
 
for i in range(m):
  for j in range(bc[i][0]):
    temp = heapq.heappop(a)
    temp2 = bc[i][1]
    if temp2 > temp:
      heapq.heappush(a, temp2)
    else:
      heapq.heappush(a, temp)
      print(sum(a))
      quit()
      
print(sum(a))

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

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]
heapq.heapify(a)

for i in range(m):
  for j in range(bc[i][0]):
    temp = heapq.heappop(a)
    temp2 = bc[i][1]
    if temp2 > temp:
      heapq.heappush(a, temp2)
    else:
      heapq.heappush(a, temp)
      break
      
print(sum(a))

Fixed submission:
import heapq
 
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]
bc.sort(key=lambda x: x[1], reverse = True)
heapq.heapify(a)
 
for i in range(m):
  for j in range(bc[i][0]):
    temp = heapq.heappop(a)
    temp2 = bc[i][1]
    if temp2 > temp:
      heapq.heappush(a, temp2)
    else:
      heapq.heappush(a, temp)
      print(sum(a))
      quit()
      
print(sum(a))

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

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]
heapq.heapify(a)

for i in range(m):
  for j in range(bc[i][0]):
    temp = heapq.heappop(a)
    temp2 = bc[i][1]
    if temp2 > temp:
      heapq.heappush(a, temp2)
    else:
      heapq.heappush(a, temp)
      break
      
print(sum(a))

Fixed submission:
import heapq
 
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]
bc.sort(key=lambda x: x[1], reverse = True)
heapq.heapify(a)
 
for i in range(m):
  for j in range(bc[i][0]):
    temp = heapq.heappop(a)
    temp2 = bc[i][1]
    if temp2 > temp:
      heapq.heappush(a, temp2)
    else:
      heapq.heappush(a, temp)
      print(sum(a))
      quit()
      
print(sum(a))