Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted(map(int, input().split()))
L = []
for _ in range(M):
    b, c = map(int, input().split())
    for _ in range(b):
        L.append(c)
L.sort(reverse=True)

idx = 0
for i in range(N):
    if (idx > len(L) - 1):
        break
    if (A[i] < L[idx]):
        A[i] = L[idx]
        idx += 1
    else:
        break

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = sorted(map(int, input().split()))
L = []
for _ in range(M):
    b, c = map(int, input().split())
    L.append([b, c])
L.sort(key=lambda x: x[1], reverse=True)
# 変更前のカードの枚数
# 変更後のカードの枚数
# をそれぞれ保存しておく

idx = 0
for i in range(N):
    if (idx > len(L) - 1):
        break
    if (A[i] < L[idx][1]):
        A[i] = L[idx][1]
        L[idx][0] -= 1
        if (L[idx][0] != 0):
            continue
        else:
            idx += 1
    else:
        break
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted(map(int, input().split()))
L = []
for _ in range(M):
    b, c = map(int, input().split())
    L.append([b, c])

L.sort(key=lambda x: x[1], reverse=True)

# 変更前のカードの枚数
# 変更後のカードの枚数
# をそれぞれ保存しておく
ans = []
remain = []
idx = 0
able_change = 0
check = False
for i in range(M):
    able_change = L[i][0]
    while True:
        if (able_change == 0 or idx > len(A) - 1):
            remain = A[idx:]
            break
        if (L[i][1] > A[idx]):
            ans.append(L[i][1])
            able_change -= 1

        else:
            # print(idx, able_change)
            check = True
            remain = A[idx:]
            break
        idx += 1

    if (check):
        break
    able_change = 0

ans_sum = sum(ans)
remain_sum = sum(remain)

print(ans_sum + remain_sum)


Fixed submission:
N, M = map(int, input().split())
A = sorted(map(int, input().split()))
L = []
for _ in range(M):
    b, c = map(int, input().split())
    L.append([b, c])
L.sort(key=lambda x: x[1], reverse=True)
# 変更前のカードの枚数
# 変更後のカードの枚数
# をそれぞれ保存しておく

idx = 0
for i in range(N):
    if (idx > len(L) - 1):
        break
    if (A[i] < L[idx][1]):
        A[i] = L[idx][1]
        L[idx][0] -= 1
        if (L[idx][0] != 0):
            continue
        else:
            idx += 1
    else:
        break
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted(map(int, input().split()))
L = []
for _ in range(M):
    b, c = map(int, input().split())
    L.append([b, c])

L.sort(key=lambda x: x[1], reverse=True)

# 変更前のカードの枚数
# 変更後のカードの枚数
# をそれぞれ保存しておく
ans = []
remain = []
idx = 0
able_change = 0
check = False
for i in range(M):
    able_change = L[i][0]
    while True:
        if (able_change == 0 or idx > len(A) - 1):
            remain = A[idx:]
            break
        if (L[i][1] > A[idx]):
            ans.append(L[i][1])
            able_change -= 1

        else:
            # print(idx, able_change)
            check = True
            remain = A[idx:]
            break
        idx += 1

    if (check):
        break
    able_change = 0

ans_sum = sum(ans)
remain_sum = sum(remain)

print(ans_sum + remain_sum)


Fixed submission:
N, M = map(int, input().split())
A = sorted(map(int, input().split()))
L = []
for _ in range(M):
    b, c = map(int, input().split())
    L.append([b, c])
L.sort(key=lambda x: x[1], reverse=True)
# 変更前のカードの枚数
# 変更後のカードの枚数
# をそれぞれ保存しておく

idx = 0
for i in range(N):
    if (idx > len(L) - 1):
        break
    if (A[i] < L[idx][1]):
        A[i] = L[idx][1]
        L[idx][0] -= 1
        if (L[idx][0] != 0):
            continue
        else:
            idx += 1
    else:
        break
print(sum(A))