Problem p02618 (python)

Fixed submission:
D = int(input())
c = [0] + list(map(int, input().split()))
s = [0] + [[0] + list(map(int, input().split())) for _ in range(D)]
last = [0]*27

for d in range(1, D+1):
    SLvar = [s[d][i] + c[i] * (d - last[i]) for i in range(1, 27)]

    t = SLvar.index(max(SLvar)) + 1
    print(t)
    last[t] = d

Buggy submission (verdict: Runtime Error):
import bisect
import copy
import time

start = time.time()

D = int(input())
c = [0] + list(map(int, input().split()))
s = [0] + [[0] + list(map(int, input().split())) for _ in range(D)]
sch, held = [0], [[0] for _ in range(27)]

for d in range(1, D+1):
    SLvar = [s[d][i] + c[i] * (d - held[i][-1]) for i in range(1, 27)]
    t = SLvar.index(max(SLvar)) + 1
    sch.append(t)
    held[t].append(d)

for i in range(1, 27):
    held[i].append(D+1)

s_sort = [0] + [[0] + sorted([[s[d][i], i] for i in range(1, 27)], key=lambda x: x[0], reverse=True) for d in range(1, D+1)]

sch_can = [[copy.copy(sch), copy.copy(held)]] + [0]*9
SL, N = [0]*10, 1

i, j, NN = 1, 1, 0
while time.time() - start < 1.8 and i <= D and j < 27:
    d, q = i, s_sort[i][j][1]

    for n in range(N):
        sch, held = sch_can[n][0], sch_can[n][1]
        print(len(sch), len(held))
        if sch_can[n][0][d] != q:
            SLvar = s[d][q] - s[d][sch_can[n][0][d]]

            ind_p = bisect.bisect_left(sch_can[n][1][sch_can[n][0][d]], d)
            k = d - sch_can[n][1][sch_can[n][0][d]][ind_p-1]
            l = sch_can[n][1][sch_can[n][0][d]][ind_p+1] - d
            SLvar -= c[sch_can[n][0][d]] * k * l

            ind_n = bisect.bisect_left(sch_can[n][1][q], d)
            k = d - sch_can[n][1][q][ind_n-1]
            l = sch_can[n][1][q][ind_n] - d
            SLvar += c[q] * k * l

            if N + NN < 10:
                sch_can[N + NN] = [copy.copy(sch_can[n][0]), copy.copy(sch_can[n][1])]
                del sch_can[n][1][sch_can[N + NN][0][d]][ind_p]
                sch_can[N + NN][1][q].insert(ind_n, d)
                sch_can[N + NN][0][d] = q
                SL[N + NN] = SL[n] + SLvar
                NN += 1
            else:
                if SL[n] + SLvar > min(SL):
                    ind = SL.index(min(SL))
                    sch_can[ind] = [copy.copy(sch_can[n][0]), copy.copy(sch_can[n][1])]
                    del sch_can[n][1][sch_can[ind][0][d]][ind_p]
                    sch_can[ind][1][q].insert(ind_n, d)
                    sch_can[ind][0][d] = q
                    SL[ind] = SL[n] + SLvar
    N += NN
    NN = 0
    i += 1
    if i > D:
        i = 1
        j += 1

for i in range(1, D+1):
    print(sch[i])

Buggy submission (verdict: Runtime Error):
import bisect
import time

start = time.time()

D = int(input())
c = [0] + list(map(int, input().split()))
s = [0] + [[0] + list(map(int, input().split())) for _ in range(D)]
sch, held = [0], [[0] for _ in range(27)]

for d in range(1, D+1):
    SLvar = [s[d][i] + c[i] * (d - held[i][-1]) for i in range(1, 27)]
    t = SLvar.index(max(SLvar)) + 1
    sch.append(t)
    held[t].append(d)

for i in range(1, 27):
    held[i].append(D+1)

s_sort = [0] + [[0] + sorted([[s[d][i], i] for i in range(1, 27)], key=lambda x: x[0], reverse=True) for d in range(1, D+1)]

i, j = 1, 1
while time.time() - start < 1.8 and i <= D and j < 27:
    d, q = i, s_sort[i][j][1]

    if sch[d] != q:
        SLvar = s[d][q] - s[d][sch[d]]

        ind = bisect.bisect_left(held[sch[d]], d)
        k = d - held[sch[d]][ind-1]
        l = held[sch[d]][ind+1] - d
        SLvar -= c[sch[d]] * k * l

        ind = bisect.bisect_left(held[q], d)
        k = d - held[q][ind-1]
        l = held[q][ind] - d
        SLvar += c[q] * k * l

        if SLvar > 0:
            del held[sch[d]][ind]
            held[q].insert(ind, d)
            sch[d] = q

    i += 1
    if i > D:
        i = 1
        j += 1

for i in range(1, D+1):
    print(sch[i])