Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def main():
	N, M = map(int, input().split())
	A = list(map(int, input().split()))
	heapq.heapify(A)
	BC = [0]*M
	for i in range(M):
		BC[i] = list(map(int, input().split()))
	BC.sort(key = lambda x: -x[1])
	for i in range(M):
		if A[0] > BC[i][1]:
			break
		for _ in range(BC[i][0]):
			if A[0] > BC[i][1]:
				break
			heapq.heappop(A)
			heapq.heappush(A, BC[i][1])
	print(sum(A))


if __name__ == '__main__':
	main()

Fixed submission:
import heapq
def main():
	N, M = map(int, input().split())
	A = list(map(int, input().split()))
	heapq.heapify(A)
	BC = [0]*M
	for i in range(M):
		BC[i] = list(map(int, input().split()))
	BC.sort(key = lambda x: -x[1])
	for B, C in BC:
		if A[0] >= C:
			break
		for _ in range(B):
			if A[0] >= C:
				break
			heapq.heapreplace(A, C)
	print(sum(A))


if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def main():
	N, M = map(int, input().split())
	A = list(map(int, input().split()))
	heapq.heapify(A)
	BC = [0]*M
	for i in range(M):
		BC[i] = list(map(int, input().split()))
	BC.sort(key = lambda x: -x[1])
	for i in range(M):
		if A[0] > BC[i][1]:
			break
		for _ in range(BC[i][0]):
			if A[0] > BC[i][1]:
				break
			heapq.heappop(A)
			heapq.heappush(A, BC[i][1])
	print(sum(A))


if __name__ == '__main__':
	main()

Fixed submission:
import heapq
def main():
	N, M = map(int, input().split())
	A = list(map(int, input().split()))
	heapq.heapify(A)
	BC = [0]*M
	for i in range(M):
		BC[i] = list(map(int, input().split()))
	BC.sort(key = lambda x: -x[1])
	for B, C in BC:
		if A[0] >= C:
			break
		for _ in range(B):
			if A[0] >= C:
				break
			heapq.heapreplace(A, C)
	print(sum(A))


if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def main():
	N, M = map(int, input().split())
	A = list(map(int, input().split()))
	heapq.heapify(A)
	for _ in range(M):
		B, C = list(map(int, input().split()))
		for _ in range(B):
			if A[0] > C:
				break
			heapq.heappop(A)
			heapq.heappush(A, C)
	print(sum(A))


if __name__ == '__main__':
	main()

Fixed submission:
import heapq
def main():
	N, M = map(int, input().split())
	A = list(map(int, input().split()))
	heapq.heapify(A)
	BC = [0]*M
	for i in range(M):
		BC[i] = list(map(int, input().split()))
	BC.sort(key = lambda x: -x[1])
	for B, C in BC:
		if A[0] >= C:
			break
		for _ in range(B):
			if A[0] >= C:
				break
			heapq.heapreplace(A, C)
	print(sum(A))


if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def main():
	N, M = map(int, input().split())
	A = list(map(int, input().split()))
	heapq.heapify(A)
	for _ in range(M):
		B, C = list(map(int, input().split()))
		for _ in range(B):
			if A[0] > C:
				break
			heapq.heappop(A)
			heapq.heappush(A, C)
	print(sum(A))


if __name__ == '__main__':
	main()

Fixed submission:
import heapq
def main():
	N, M = map(int, input().split())
	A = list(map(int, input().split()))
	heapq.heapify(A)
	BC = [0]*M
	for i in range(M):
		BC[i] = list(map(int, input().split()))
	BC.sort(key = lambda x: -x[1])
	for B, C in BC:
		if A[0] >= C:
			break
		for _ in range(B):
			if A[0] >= C:
				break
			heapq.heapreplace(A, C)
	print(sum(A))


if __name__ == '__main__':
	main()