Problem p02618 (python)

Fixed submission:
import numpy as np

D = int(input())
Cs = list(map(int, input().split()))
Ss = []
for d in range(D):
    s = list(map(int, input().split()))
    Ss.append(s)

Ss = np.array(Ss)

solution = np.argmax(Ss, axis=1)
#
# days_till_next = np.ones_like(Ss) * (D - 1) - np.arange(D).reshape((D, 1))
#
#
# def update_days_till_next(d):
#     contest = solution[d]
#     for _d in range(d - 1, -1, -1):
#         if solution[_d] == contest or _d == 0:
#             days_till_next[_d:d, contest] -= days_till_next[d, contest]
#             break
#
#
# for d in range(D - 1, -1, -1):
#     update_days_till_next(d)
#
# for _ in range(10):
#     n_contests = len(Ss[0])
#     days_since_last = np.zeros(n_contests)
#     for d, i in enumerate(solution):
#         days_since_last += 1
#         new_scores = days_till_next[d] * days_since_last[d] * Cs + Ss[d]
#         new_i = new_scores.argmax()
#         days_since_last[new_i] = 0
#         if new_i != i:
#             solution[d] = new_i
#             update_days_till_next(d)
#

for s in solution:
    print(s + 1)

Buggy submission (verdict: Runtime Error):
import numpy as np

D = int(input())
Cs = list(map(int, input().split()))
Ss = []
for d in range(D):
    s = list(map(int, input().split()))
    Ss.append(s)

Ss = np.array(Ss)


def scoring(solution):
    sat = 0
    last = [0] * 26
    for d in range(D):
        assert 1 <= solution[d] <= 26
        j = solution[d] - 1
        last[j] = d + 1
        for i in range(26):
            sat -= (d + 1 - last[i]) * Cs[i]
        sat += Ss[d][j]
    return sat


def update_days_till_next(days_till_next, solution, d, erase=False):
    contest = solution[d]
    delta = days_till_next[d + 1, contest] + 1 if d < D - 1 else 0
    days_till_next[d, contest] = delta
    for _d in range(d - 1, -1, -1):
        if solution[_d] == contest or _d == 0:
            days_till_next[_d:d, contest] = (d - _d) - np.arange(d - _d) + (delta if erase else 0)
            break


def get_solution(seed):
    solution = np.array(seed)
    days_till_next = np.ones_like(Ss) * (D - 1) - np.arange(D).reshape((D, 1))
    for d in range(D - 1, -1, -1):
        update_days_till_next(days_till_next, solution, d)

    for _ in range(80):
        n_contests = len(Ss[0])
        days_since_last = np.zeros(n_contests)
        for d, i in enumerate(solution):
            days_since_last += 1
            new_scores = days_till_next[d] * days_since_last * Cs + Ss[d]
            new_i = new_scores.argmax()
            days_since_last[new_i] = 0
            if new_i != i:
                update_days_till_next(days_till_next, solution, d, erase=True)
                solution[d] = new_i
                update_days_till_next(days_till_next, solution, d)

    return solution


def get_best_solution():
    seeds = [np.random.randint(0, 26, D) for _ in range(4)] + [np.argmax(Ss, axis=1)]
    best_sol = None
    best_score = 0
    for seed in seeds:
        sol = get_solution(seed) + 1
        score = scoring(sol)
        if score > best_score:
            best_sol = sol

    return best_sol


solution = get_best_solution()


for s in solution:
    print(s)

with open("test.out", "w") as f:
    f.writelines("\n".join(list(map(str, solution))))

Buggy submission (verdict: Runtime Error):
import numpy as np

D = int(input())
Cs = list(map(int, input().split()))
Ss = []
for d in range(D):
    s = list(map(int, input().split()))
    Ss.append(s)

Ss = np.array(Ss)

solution = np.argmax(Ss, axis=1)

days_till_next = np.ones_like(Ss) * (D - 1) - np.arange(D).reshape((D, 1))


def update_days_till_next(d):
    contest = solution[d]
    for _d in range(d - 1, -1, -1):
        if solution[_d] == contest or _d == 0:
            days_till_next[_d:d, contest] -= days_till_next[d, contest]
            break


for d in range(D - 1, -1, -1):
    update_days_till_next(d)

for _ in range(10):
    n_contests = len(Ss[0])
    days_since_last = np.zeros(n_contests)
    for d, i in enumerate(solution):
        days_since_last += 1
        new_scores = days_till_next[d] * days_since_last[d] * Cs + Ss[d]
        new_i = new_scores.argmax()
        days_since_last[new_i] = 0
        if new_i != i:
            solution[d] = new_i
            update_days_till_next(d)


for s in solution:
    print(s + 1)

Buggy submission (verdict: Runtime Error):
import numpy as np

D = int(input())
Cs = list(map(int, input().split()))
Ss = []
for d in range(D):
    s = list(map(int, input().split()))
    Ss.append(s)

Ss = np.array(Ss)

solution = np.argmax(Ss, axis=1)

days_till_next = np.ones_like(Ss) * (D - 1) - np.arange(D).reshape((D, 1))


def update_days_till_next(d):
    for _d in range(d - 1, -1, -1):
        if solution[_d] == contest or _d == 0:
            days_till_next[_d:d, contest] -= days_till_next[d, contest]
            break


for d in range(D - 1, -1, -1):
    contest = solution[d]
    update_days_till_next(d)

for _ in range(10):
    n_contests = len(Ss[0])
    days_since_last = np.zeros(n_contests)
    for d, i in enumerate(solution):
        days_since_last += 1
        new_scores = days_till_next[d] * days_since_last[d] * Cs + Ss[d]
        new_i = new_scores.argmax()
        days_since_last[new_i] = 0
        if new_i != i:
            solution[d] = new_i
            update_days_till_next(d)


for s in solution:
    print(s + 1)