Problem p02618 (python)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
import numpy as np

def scores(c, s, t):
    last = [0] * 26
    V = []
    lastv = 0
    for d, i in enumerate(t):
        last[i] = d+1
        v = s[d*26+i]
        sub = sum([c[j]*(d+1-last[j]) for j in range(26)])
        # print(v, sub)
        lastv += v-sub
        V.append(lastv)
    return V


def main():
    D, *cs = map(int, read().split())
    c = cs[:26]
    s = cs[26:]

    b = 0
    ans = []
    for d in range(D):
        maxi = 0
        for i in range(26):
            if s[b+i] > s[b+maxi]:
                maxi = i
        ans.append(maxi + 1)
        b += 26

    print(*ans, sep='\n')

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
import numpy as np

def scores(c, s, t):
    last = [0] * 26
    V = []
    lastv = 0
    D = len(t)
    for d, i in enumerate(t):
        last[i] = d+1
        if d != D-1:
            continue
        v = s[d*26+i]
        sub = sum([c[j]*(d+1-last[j]) for j in range(26)])
        # print(v, sub)
        lastv += v-sub
        V.append(lastv)
    return V


def main():
    D, *cs = map(int, read().split())
    c = cs[:26]
    s = cs[26:]

    b = 0
    ans = []
    for d in range(D):
        maxi = 0
        score_max = -1000000000
        for i in range(26):
            if scores(c, s, ans + [i])[-1] > score_max:
                maxi = i
        ans.append(maxi)
        b += 26

    ans = [a+1 for a in ans]
    print(*ans, sep='\n')

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from bisect import bisect
from bisect import bisect_right
from bisect import bisect_left
from bisect import insort
from bisect import insort_right
from bisect import insort_left 
import random

def scores(c, s, t, t_days):
    last = [0] * 26
    D = len(t)
    t_score = []
    # print([s[d*26+i] for d, i in enumerate(t)])
    s_score = sum([s[d*26+i] for d, i in enumerate(t)])
    for i, days in enumerate(t_days):
        last = 0
        score = 0
        for d in days[1:]:
            delta = d - last - 1
            # print(i, d, delta)
            score += delta * (delta + 1)
            last = d
        # print(i, score)
        score = score * c[i] // 2
        t_score.append(score)
    # print(t_score)
    return s_score - sum(t_score)

def main():
    D, *cs = map(int, read().split())
    c = cs[:26]
    se = 26+26*D
    s = cs[26:se]

    t = []
    for d in range(D):
        maxi = 0
        for i in range(26):
            if s[b+i] > s[b+maxi]:
                maxi = i
        t.append(maxi + 1)
        b += 26


    t_days = [[0] for i in range(26)]
    for d, x in enumerate(t, start=1):
        t_days[x].append(d)
    for i in range(26):
        t_days[i].append(D+1)
    # print(t_days)

    v = scores(c, s, t, t_days)
    maxv = v
    # print(v)
    ans = t[:]
    for i in range(1000):
        d = random.randint(0, D)
        q = random.randint(0, 25)

        dd = d-1
        bq = t[dd]
        t[dd] = q
        v += s[dd*26+q] - s[dd*26+bq]
        days_q = t_days[q]
        days_bq = t_days[bq]
        qi = bisect_left(days_q, d)
        bqi = bisect_left(days_bq, d)
        # print(bq, bqi, q, qi)

        # 削除することで増える分
        da = days_bq[bqi+1] - days_bq[bqi-1] - 1
        db1 = days_bq[bqi+1] - days_bq[bqi] - 1
        db2 = days_bq[bqi] - days_bq[bqi-1] - 1
        bqp = da * (da + 1) - (db1*(db1+1) + db2*(db2+1))
        # 追加することで減る分
        db = days_q[qi] - days_q[qi-1] - 1
        da1 = days_q[qi] - d - 1
        da2 = d - days_q[qi-1] - 1
        qp = db * (db + 1) - (da1*(da1+1) + da2*(da2+1))

        # print((bqp * c[bq] // 2))
        # print((qp * c[q] // 2))
        delta = (qp * c[q] // 2) - (bqp * c[bq] // 2)

        # print(days_bq, days_q)
        insort_left(days_q, d)
        del(days_bq[bqi])
        # print(days_bq, days_q)
        v += delta
        # print(v, delta)
        if maxv < v:
            ans = t[:]
        # ans.append(v)
        # t_days[bq].remove(bq)

        # qの不満度が減る分



        # last = 0
        # score = 0
        # for d in t_days[q] + [D+1]:
        #     delta = d - last - 1
        #     # print(i, d, delta)
        #     score += delta * (delta + 1)
        #     last = d
        # # print(i, score)
        # score = score * c[q] // 2


    print(*ans, sep='\n')

if __name__ == '__main__':
    main()