Buggy python submission (verdict: Time Limit Exceeded):
inf = 10**12


def main():
    import sys
    from random import randint
    input = sys.stdin.readline

    class Bit:
        def __init__(self, n):
            self.size = n
            self.tree = [0] * (n + 1)

        def sum(self, i):
            s = 0
            while i > 0:
                s += self.tree[i]
                i -= i & -i
            return s

        def add(self, i, x):
            while i <= self.size:
                self.tree[i] += x
                i += i & -i

        def lower_bound(self, w):
            if w <= 0:
                return 0
            x = 0
            k = 1 << (self.size.bit_length() - 1)
            while k:
                if x + k <= self.size and self.tree[x + k] < w:
                    w -= self.tree[x + k]
                    x += k
                k >>= 1
            return x + 1

    D = int(input())
    C = list(map(int, input().split()))
    S = [[]]
    for _ in range(D):
        S.append(list(map(int, input().split())))
    ans = 0
    last = [0] * 26
    days = [Bit(D+1) for _ in range(26)]
    for i in range(26):
        days[i].add(D+1, 1)
    T = [-1]
    # greedy
    for d in range(1, D+1):
        plus = -inf
        t = -1
        for tt in range(26):
            plus_new = S[d][tt] + C[tt] * (d - last[tt])
            if plus_new > plus:
                t = tt
                plus = plus_new
        T.append(t)
        ans += S[d][t]
        last[t] = d
        days[t].add(d, 1)
        for i in range(26):
            ans -= C[i] * (d - last[i])

    def calc(d_list, q_list, cur):
        for d, q in zip(d_list, q_list):
            t = T[d]
            cur += S[d][q]
            cur -= S[d][t]
            k = days[q].sum(d)
            i = days[q].lower_bound(k)
            j = days[q].lower_bound(k+1)
            cur += C[q] * (d - i) * (j - d)
            k = days[t].sum(d)
            i = days[t].lower_bound(k-1)
            j = days[t].lower_bound(k+1)
            cur -= C[t] * (d - i) * (j - d)

            T[d] = q
            days[q].add(d, 1)
            days[t].add(d, -1)
        return cur


    # climb
    size = 1
    try_num = 200
    loop_num = 13
    for _ in range(loop_num):
        for x in range(D // size):
            d_list = list(range(x*size+1, (x+1)*size+1))
            for _ in range(try_num):
                t_new = [randint(0, 25) for _ in range(size)]
                for i, d in enumerate(d_list):
                    if t_new[i] == T[d]:
                        t_new[i] = (t_new[i] + 1)%26
                t_ori = []
                for d in d_list:
                    t_ori.append(T[d])
                ans_new = calc(d_list, t_new, ans)
                if ans_new < ans:
                    for i in range(size-1, -1, -1):
                        d = d_list[i]
                        T[d] = t_ori[i]
                        days[t_ori[i]].add(d, 1)
                        days[t_new[i]].add(d, -1)
                else:
                    ans = ans_new
    # print(ans)
    for t in T[1:]:
        print(t+1)


if __name__ == '__main__':
    main()


Fixed submission:
inf = 10**12


def main():
    import sys
    from bisect import bisect_left
    input = sys.stdin.readline

    D = int(input())
    C = list(map(int, input().split()))
    S = []
    for _ in range(D):
        S.append(list(map(int, input().split())))
    ans = 0
    last = [-1] * 26
    days = [[-1] for _ in range(26)]
    T = []
    # greedy
    for d in range(D):
        plus = -inf
        t = -1
        for tt in range(26):
            plus_new = S[d][tt] + C[tt] * (d - last[tt])
            if plus_new > plus:
                t = tt
                plus = plus_new
        T.append(t)
        ans += S[d][t]
        last[t] = d
        days[t].append(d)
        for i in range(26):
            ans -= C[i] * (d - last[i])
    for t in range(26):
        days[t].append(D)
    for t in T:
        print(t+1)
    exit()







    M = int(input())
    for _ in range(M):
        d, q = map(int, input().split())
        d -= 1
        q -= 1
        t = T[d]
        ans += S[d][q]
        ans -= S[d][t]
        i = bisect_left(days[q], d)
        ans += C[q] * (d - days[q][i-1]) * (days[q][i] - d)
        j = bisect_left(days[t], d)
        ans -= C[t] * (d - days[t][j-1]) * (days[t][j+1] - d)
        #print(days[q], days[t])
        T[d] = q
        days[q].append(d)
        days[q].sort()
        days[t].remove(d)
        print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
inf = 10**12


def main():
    import sys
    from random import randint
    from bisect import bisect_left
    input = sys.stdin.readline

    D = int(input())
    C = list(map(int, input().split()))
    S = []
    for _ in range(D):
        S.append(list(map(int, input().split())))
    ans = 0
    last = [-1] * 26
    days = [[-1] for _ in range(26)]
    T = []
    # greedy
    for d in range(D):
        plus = -inf
        t = -1
        for tt in range(26):
            plus_new = S[d][tt] + C[tt] * (d - last[tt])
            if plus_new > plus:
                t = tt
                plus = plus_new
        T.append(t)
        ans += S[d][t]
        last[t] = d
        days[t].append(d)
        for i in range(26):
            ans -= C[i] * (d - last[i])
    for t in range(26):
        days[t].append(D)

    def calc(d, q, cur):
        t = T[d]
        cur += S[d][q]
        cur -= S[d][t]
        i = bisect_left(days[q], d)
        cur += C[q] * (d - days[q][i - 1]) * (days[q][i] - d)
        j = bisect_left(days[t], d)
        cur -= C[t] * (d - days[t][j - 1]) * (days[t][j + 1] - d)
        return cur

    # climb
    size = 5
    try_num = 1000
    for x in range(D // size):
        d_list = list(range(x*size, (x+1)*size))
        for _ in range(try_num):
            t_new = [randint(0, 25) for _ in range(size)]
            ans_new = ans
            for d in d_list:
                ans_new = calc(d, t_new[d], ans_new)
            if ans_new > ans:
                ans = ans_new
                for d in d_list:
                    t_old = T[d]
                    T[d] = t_new[d]
                    days[t_new[d]].append(d)
                    days[t_new[d]].sort()
                    days[t_old].remove(d)
    for t in T:
        print(t+1)


if __name__ == '__main__':
    main()


Fixed submission:
inf = 10**12


def main():
    import sys
    from bisect import bisect_left
    input = sys.stdin.readline

    D = int(input())
    C = list(map(int, input().split()))
    S = []
    for _ in range(D):
        S.append(list(map(int, input().split())))
    ans = 0
    last = [-1] * 26
    days = [[-1] for _ in range(26)]
    T = []
    # greedy
    for d in range(D):
        plus = -inf
        t = -1
        for tt in range(26):
            plus_new = S[d][tt] + C[tt] * (d - last[tt])
            if plus_new > plus:
                t = tt
                plus = plus_new
        T.append(t)
        ans += S[d][t]
        last[t] = d
        days[t].append(d)
        for i in range(26):
            ans -= C[i] * (d - last[i])
    for t in range(26):
        days[t].append(D)
    for t in T:
        print(t+1)
    exit()







    M = int(input())
    for _ in range(M):
        d, q = map(int, input().split())
        d -= 1
        q -= 1
        t = T[d]
        ans += S[d][q]
        ans -= S[d][t]
        i = bisect_left(days[q], d)
        ans += C[q] * (d - days[q][i-1]) * (days[q][i] - d)
        j = bisect_left(days[t], d)
        ans -= C[t] * (d - days[t][j-1]) * (days[t][j+1] - d)
        #print(days[q], days[t])
        T[d] = q
        days[q].append(d)
        days[q].sort()
        days[t].remove(d)
        print(ans)


if __name__ == '__main__':
    main()