Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
for i in range(M):
    b, c = map(int, input().split())
    for j in range(b):
        if A[0] < c:
            tmp = heapq.heappop(A)
            heapq.heappush(A, c)
print(sum(A))

Fixed submission:
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
l = []
for i in range(M):
    b, c = map(int, input().split())
    l.append([b, c])
l.sort(key=lambda x: x[1], reverse=True)
A.sort(reverse=False)
idx = 0
i = 0
ans = 0
while i < N and idx < M:
    if l[idx][0] == 0:
        idx += 1
    if idx >= M:
        ans += A[i]
        i += 1
        continue
    if A[i] < l[idx][1]:
        ans += l[idx][1]
        l[idx][0] -= 1
        i += 1
    else:
        ans += A[i]
        i += 1
    # print("ans =", ans)
for j in range(i, N):
    ans += A[j]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
for i in range(M):
    b, c = map(int, input().split())
    for j in range(b):
        if A[0] < c:
            tmp = heapq.heappop(A)
            heapq.heappush(A, c)
print(sum(A))

Fixed submission:
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
l = []
for i in range(M):
    b, c = map(int, input().split())
    l.append([b, c])
l.sort(key=lambda x: x[1], reverse=True)
A.sort(reverse=False)
idx = 0
i = 0
ans = 0
while i < N and idx < M:
    if l[idx][0] == 0:
        idx += 1
    if idx >= M:
        ans += A[i]
        i += 1
        continue
    if A[i] < l[idx][1]:
        ans += l[idx][1]
        l[idx][0] -= 1
        i += 1
    else:
        ans += A[i]
        i += 1
    # print("ans =", ans)
for j in range(i, N):
    ans += A[j]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
for i in range(M):
    b, c = map(int, input().split())
    for j in range(b):
        tmp = heapq.heappop(A)
        if c > tmp:
            heapq.heappush(A, c)
        else:
            heapq.heappush(A, tmp)
print(sum(A))

Fixed submission:
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
l = []
for i in range(M):
    b, c = map(int, input().split())
    l.append([b, c])
l.sort(key=lambda x: x[1], reverse=True)
A.sort(reverse=False)
idx = 0
i = 0
ans = 0
while i < N and idx < M:
    if l[idx][0] == 0:
        idx += 1
    if idx >= M:
        ans += A[i]
        i += 1
        continue
    if A[i] < l[idx][1]:
        ans += l[idx][1]
        l[idx][0] -= 1
        i += 1
    else:
        ans += A[i]
        i += 1
    # print("ans =", ans)
for j in range(i, N):
    ans += A[j]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
for i in range(M):
    b, c = map(int, input().split())
    for j in range(b):
        tmp = heapq.heappop(A)
        if c > tmp:
            heapq.heappush(A, c)
        else:
            heapq.heappush(A, tmp)
print(sum(A))

Fixed submission:
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
l = []
for i in range(M):
    b, c = map(int, input().split())
    l.append([b, c])
l.sort(key=lambda x: x[1], reverse=True)
A.sort(reverse=False)
idx = 0
i = 0
ans = 0
while i < N and idx < M:
    if l[idx][0] == 0:
        idx += 1
    if idx >= M:
        ans += A[i]
        i += 1
        continue
    if A[i] < l[idx][1]:
        ans += l[idx][1]
        l[idx][0] -= 1
        i += 1
    else:
        ans += A[i]
        i += 1
    # print("ans =", ans)
for j in range(i, N):
    ans += A[j]
print(ans)

Buggy python submission (verdict: Runtime Error):
#coding:utf-8
n, m = map(int, input().split())
A = list(map(int, input().split()))
ll = []
for i in range(m):
    b, c = map(int, input().split())
    ll.extend([c] * b)
A.sort(reverse=True)
ll.sort(reverse=True)
i, j = 0, 0
ans = 0
for _ in range(n):
    if A[i] >= ll[j] and i < n:
        ans += A[i]
        i += 1
    elif A[i] < ll[j] and j < n:
        ans += ll[j]
        j += 1
    else:
        ans += A[i]
        i += 1
print(ans)

Fixed submission:
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
l = []
for i in range(M):
    b, c = map(int, input().split())
    l.append([b, c])
l.sort(key=lambda x: x[1], reverse=True)
A.sort(reverse=False)
idx = 0
i = 0
ans = 0
while i < N and idx < M:
    if l[idx][0] == 0:
        idx += 1
    if idx >= M:
        ans += A[i]
        i += 1
        continue
    if A[i] < l[idx][1]:
        ans += l[idx][1]
        l[idx][0] -= 1
        i += 1
    else:
        ans += A[i]
        i += 1
    # print("ans =", ans)
for j in range(i, N):
    ans += A[j]
print(ans)

Buggy python submission (verdict: Runtime Error):
#coding:utf-8
n, m = map(int, input().split())
A = list(map(int, input().split()))
ll = []
for i in range(m):
    b, c = map(int, input().split())
    ll.extend([c] * b)
A.sort(reverse=True)
ll.sort(reverse=True)
i, j = 0, 0
ans = 0
for _ in range(n):
    if A[i] > ll[j]:
        ans += A[i]
        i += 1
    else:
        ans += ll[j]
        j += 1
print(ans)
    

Fixed submission:
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
l = []
for i in range(M):
    b, c = map(int, input().split())
    l.append([b, c])
l.sort(key=lambda x: x[1], reverse=True)
A.sort(reverse=False)
idx = 0
i = 0
ans = 0
while i < N and idx < M:
    if l[idx][0] == 0:
        idx += 1
    if idx >= M:
        ans += A[i]
        i += 1
        continue
    if A[i] < l[idx][1]:
        ans += l[idx][1]
        l[idx][0] -= 1
        i += 1
    else:
        ans += A[i]
        i += 1
    # print("ans =", ans)
for j in range(i, N):
    ans += A[j]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
def main():
    n, m = map(int, input().split())
    A = list(map(int, input().split()))
    for i in range(m):
        b, c = map(int, input().split())
        cnt = 0
        A.sort()
        for j in range(b):
            if A[j] < c:
                cnt += 1
            else:
                break
        A[:cnt] = [c] * cnt
    print(sum(A))
if __name__ == "__main__":
    main()

Fixed submission:
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
l = []
for i in range(M):
    b, c = map(int, input().split())
    l.append([b, c])
l.sort(key=lambda x: x[1], reverse=True)
A.sort(reverse=False)
idx = 0
i = 0
ans = 0
while i < N and idx < M:
    if l[idx][0] == 0:
        idx += 1
    if idx >= M:
        ans += A[i]
        i += 1
        continue
    if A[i] < l[idx][1]:
        ans += l[idx][1]
        l[idx][0] -= 1
        i += 1
    else:
        ans += A[i]
        i += 1
    # print("ans =", ans)
for j in range(i, N):
    ans += A[j]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
n, m = map(int, input().split())
A = list(map(int, input().split()))
for i in range(m):
    b, c = map(int, input().split())
    cnt = 0
    A.sort()
    for j in range(b):
        if A[j] < c:
            cnt += 1
        else:
            break
    A[:cnt] = [c] * cnt
print(sum(A))


Fixed submission:
# coding: utf-8
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
minus = 0
l = []
for i in range(M):
    b, c = map(int, input().split())
    l.append([b, c])
l.sort(key=lambda x: x[1], reverse=True)
A.sort(reverse=False)
idx = 0
i = 0
ans = 0
while i < N and idx < M:
    if l[idx][0] == 0:
        idx += 1
    if idx >= M:
        ans += A[i]
        i += 1
        continue
    if A[i] < l[idx][1]:
        ans += l[idx][1]
        l[idx][0] -= 1
        i += 1
    else:
        ans += A[i]
        i += 1
    # print("ans =", ans)
for j in range(i, N):
    ans += A[j]
print(ans)