Buggy python submission (verdict: Runtime Error):
import sys

N, M = map(int, sys.stdin.readline().split())

A = list(map(int, sys.stdin.readline().split()))
action_list = []
B = []
for i in range(M):
    temp_B, temp_C = map(int, sys.stdin.readline().split())
    action_list.append([temp_B, temp_C])
    B.append(temp_B)
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
current_list.sort()

k = 0
ans_sum = 0
count = 0
action_count = 0
for i in range(sum(B)):
    if current_list[i] < action_list[k][1]:
        ans_sum += action_list[k][1]
        count += 1
        action_count += 1
        if action_count == action_list[k][0]:
            k += 1
            action_count = 0
    else:
        break
    if count > N:
        break
    if k >= M:
        break


ans_sum += sum(current_list[count:])
print(ans_sum)

Fixed submission:
import sys

N, M = map(int, sys.stdin.readline().split())

A = list(map(int, sys.stdin.readline().split()))
action_list = []
B = []
for i in range(M):
    temp_B, temp_C = map(int, sys.stdin.readline().split())
    action_list.append([temp_B, temp_C])
    B.append(temp_B)
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
current_list.sort()

k = 0
ans_sum = 0
count = 0
action_count = 0
if sum(B) < N:
    times = sum(B)
else:
    times = N
for i in range(times):
    if current_list[i] < action_list[k][1]:
        ans_sum += action_list[k][1]
        count += 1
        action_count += 1
        if action_count == action_list[k][0]:
            k += 1
            action_count = 0
    else:
        break
    if count > N:
        break

ans_sum += sum(current_list[count:])
print(ans_sum)

Buggy python submission (verdict: Runtime Error):
import sys

N, M = map(int, sys.stdin.readline().split())

A = list(map(int, sys.stdin.readline().split()))
action_list = []
B = []
for i in range(M):
    temp_B, temp_C = map(int, sys.stdin.readline().split())
    action_list.append([temp_B, temp_C])
    B.append(temp_B)
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
current_list.sort()

k = 0
ans_sum = 0
count = 0
action_count = 0
for i in range(sum(B)):
    if current_list[i] < action_list[k][1]:
        ans_sum += action_list[k][1]
        count += 1
        action_count += 1
        if action_count == action_list[k][0]:
            k += 1
            action_count = 0
    else:
        break
    if count > N:
        break

ans_sum += sum(current_list[count:])
print(ans_sum)

Fixed submission:
import sys

N, M = map(int, sys.stdin.readline().split())

A = list(map(int, sys.stdin.readline().split()))
action_list = []
B = []
for i in range(M):
    temp_B, temp_C = map(int, sys.stdin.readline().split())
    action_list.append([temp_B, temp_C])
    B.append(temp_B)
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
current_list.sort()

k = 0
ans_sum = 0
count = 0
action_count = 0
if sum(B) < N:
    times = sum(B)
else:
    times = N
for i in range(times):
    if current_list[i] < action_list[k][1]:
        ans_sum += action_list[k][1]
        count += 1
        action_count += 1
        if action_count == action_list[k][0]:
            k += 1
            action_count = 0
    else:
        break
    if count > N:
        break

ans_sum += sum(current_list[count:])
print(ans_sum)

Buggy python submission (verdict: Runtime Error):
import sys

N, M = map(int, sys.stdin.readline().split())

A = list(map(int, sys.stdin.readline().split()))
action_list = []
B = []
for i in range(M):
    temp_B, temp_C = map(int, sys.stdin.readline().split())
    action_list.append([temp_B, temp_C])
    B.append(temp_B)
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
current_list.sort()

k = 0
ans_sum = 0
count = 0
action_count = 0
for i in range(sum(B)):
    if current_list[i] < action_list[k][1]:
        ans_sum += action_list[k][1]
        count += 1
        action_count += 1
        if action_count == action_list[k][0]:
            k += 1
            action_count = 0
    else:
        break

ans_sum += sum(current_list[count:])
print(ans_sum)

Fixed submission:
import sys

N, M = map(int, sys.stdin.readline().split())

A = list(map(int, sys.stdin.readline().split()))
action_list = []
B = []
for i in range(M):
    temp_B, temp_C = map(int, sys.stdin.readline().split())
    action_list.append([temp_B, temp_C])
    B.append(temp_B)
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
current_list.sort()

k = 0
ans_sum = 0
count = 0
action_count = 0
if sum(B) < N:
    times = sum(B)
else:
    times = N
for i in range(times):
    if current_list[i] < action_list[k][1]:
        ans_sum += action_list[k][1]
        count += 1
        action_count += 1
        if action_count == action_list[k][0]:
            k += 1
            action_count = 0
    else:
        break
    if count > N:
        break

ans_sum += sum(current_list[count:])
print(ans_sum)

Buggy python submission (verdict: Runtime Error):
import sys

N, M = map(int, sys.stdin.readline().split())

A = list(map(int, sys.stdin.readline().split()))
action_list = []
B = []
for i in range(M):
    temp_B, temp_C = map(int, sys.stdin.readline().split())
    action_list.append([temp_B, temp_C])
    B.append(temp_B)
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
count = 0
current_list.sort()

k = 0
ans_sum = 0
count = 0
action_count = 0
for i in range(sum(B)):
    if current_list[i] < action_list[k][1]:
        ans_sum += action_list[k][1]
        count += 1
        action_count += 1
        if action_count == action_list[k][0]:
            k += 1
    else:
        break

ans_sum += sum(current_list[count:])
print(ans_sum)

Fixed submission:
import sys

N, M = map(int, sys.stdin.readline().split())

A = list(map(int, sys.stdin.readline().split()))
action_list = []
B = []
for i in range(M):
    temp_B, temp_C = map(int, sys.stdin.readline().split())
    action_list.append([temp_B, temp_C])
    B.append(temp_B)
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
current_list.sort()

k = 0
ans_sum = 0
count = 0
action_count = 0
if sum(B) < N:
    times = sum(B)
else:
    times = N
for i in range(times):
    if current_list[i] < action_list[k][1]:
        ans_sum += action_list[k][1]
        count += 1
        action_count += 1
        if action_count == action_list[k][0]:
            k += 1
            action_count = 0
    else:
        break
    if count > N:
        break

ans_sum += sum(current_list[count:])
print(ans_sum)

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

N, M = map(int, sys.stdin.readline().split())

A = list(map(int, sys.stdin.readline().split()))
action_list = []
for i in range(M):
    temp_B, temp_C = map(int, sys.stdin.readline().split())
    action_list.append([temp_B, temp_C])
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
count = 0
for i in range(M):
    if min(current_list) > action_list[i][1]:
        break
    if count > N:
        break
    temp_list = [e for e in current_list if e < action_list[i][1]]
    temp_list.sort()
    other_list = [e for e in current_list if e >= action_list[i][1]]
    if len(temp_list) < action_list[i][0]:
        times = len(temp_list)
    else:
        times = action_list[i][0]
    for j in range(times):
        temp_list[j] = action_list[i][1]
    current_list = temp_list + other_list
    count += action_list[i][0]

ans = sum(current_list)
print(ans)

Fixed submission:
import sys

N, M = map(int, sys.stdin.readline().split())

A = list(map(int, sys.stdin.readline().split()))
action_list = []
B = []
for i in range(M):
    temp_B, temp_C = map(int, sys.stdin.readline().split())
    action_list.append([temp_B, temp_C])
    B.append(temp_B)
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
current_list.sort()

k = 0
ans_sum = 0
count = 0
action_count = 0
if sum(B) < N:
    times = sum(B)
else:
    times = N
for i in range(times):
    if current_list[i] < action_list[k][1]:
        ans_sum += action_list[k][1]
        count += 1
        action_count += 1
        if action_count == action_list[k][0]:
            k += 1
            action_count = 0
    else:
        break
    if count > N:
        break

ans_sum += sum(current_list[count:])
print(ans_sum)