Problem p03038 (python)

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 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)

Buggy 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)

Buggy 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)

Buggy 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)