Buggy python submission (verdict: Runtime Error):
import numpy as np
from itertools import product

D = int(input())
C = np.array(list(map(int, input().split())))
S = np.array([list(map(int, input().split())) for d in range(D)])

L = np.empty(26, int)
L[:] = -1

P2 = list(product(range(26), repeat=2))
P3 = list(product(range(26), repeat=3))

for d in range(0, D, 3):
    scores = []
    for t1, t2, t3 in P3:
        LL = L.copy()

        score = 0
        for dd in range(d, d+3):
            LL[t] = dd
            score += S[d, t] - np.dot(C, dd-LL)

        scores.append(score)

    smax = scores[0]
    tmax = 0
    for t, s in enumerate(scores):
        if s>smax:
            smax = s
            tmax = t

    for tt, dd in zip(P3[tmax], range(d, d+3)):
        L[tt] = dd
        print(tt+1)

d = 363
scores = []
for t1, t2 in P2:
    LL = L.copy()

    score = 0
    for dd in range(d, d+2):
        LL[t] = dd
        score += S[d, t] - np.dot(C, dd-LL)

    scores.append(score)

smax = scores[0]
tmax = 0
for t, s in enumerate(scores):
    if s>smax:
        smax = s
        tmax = t

for tt, dd in zip(P2[tmax], range(d, d+3)):
    L[tt] = dd
    print(tt+1)


Fixed submission:
import numpy as np
from bisect import bisect_left

D = int(input())
C = np.array(list(map(int, input().split())))
S = np.array([list(map(int, input().split())) for d in range(D)])

L = np.empty(26, int)
L[:] = -1

X = [[-1] for _ in range(26)]
T = []

score = 0
for d in range(D):
    t = int(input())-1
    T.append(t)
    L[t] = d
    score += S[d, t] - np.dot(C, d-L)
    X[t].append(d)

for x in X:
    x.append(D)
    
M = int(input())
for _ in range(M):
    d, q = map(int, input().split())
    d-=1
    q-=1

    t = T[d]
    T[d] = q
    
    i = bisect_left(X[t], d)
    d_prev = X[t][i-1]
    d_next = X[t][i+1]
    
    dd = d-d_prev-1
    score += C[t]*(dd+1)*dd//2
    dd = d_next-d-1
    score += C[t]*(dd+1)*dd//2
    dd = d_next-d_prev-1
    score -= C[t]*(dd+1)*dd//2

    X[t].pop(i)
    
    i = bisect_left(X[q], d)
    d_prev = X[q][i-1]
    d_next = X[q][i]
    
    dd = d-d_prev-1
    score -= C[q]*(dd+1)*dd//2
    dd = d_next-d-1
    score -= C[q]*(dd+1)*dd//2
    dd = d_next-d_prev-1
    score += C[q]*(dd+1)*dd//2

    X[q].insert(i, d)

    score += S[d,q] - S[d,t]

    print(score)

Buggy python submission (verdict: Runtime Error):
import numpy as np
from bisect import bisect_left

D = int(input())
C = np.array(list(map(int, input().split())))
S = np.array([list(map(int, input().split())) for d in range(D)])

L = np.empty(26, int)
L[:] = -1

X = [[-1] for _ in range(26)]
T = []

score = 0
for d in range(D):
    t = int(input())-1
    T.append(t)
    L[t] = d
    score += S[d, t] - np.dot(C, d-L)
    X[t].append(d)

for x in X:
    x.append(D)
    
M = int(input())
for _ in range(M):
    d, q = map(int, input().split())
    d-=1
    q-=1

    t = T[d]
    T[d] = q
    
    i = bisect_left(X[t], d)
    d_prev = X[t][i-1]
    d_next = X[t][i+1]
    
    dd = d-d_prev-1
    score += C[t]*(dd+1)*dd//2
    dd = d_next-d-1
    score += C[t]*(dd+1)*dd//2
    dd = d_next-d_prev-1
    score -= C[t]*(dd+1)*dd//2

    i = bisect_left(X[q], d)
    d_prev = X[q][i-1]
    d_next = X[q][i]
    
    dd = d-d_prev-1
    score -= C[q]*(dd+1)*dd//2
    dd = d_next-d-1
    score -= C[q]*(dd+1)*dd//2
    dd = d_next-d_prev-1
    score += C[q]*(dd+1)*dd//2

    X[t].pop(i)
    X[q].insert(i, d)

    score += S[d,q] - S[d,t]

    print(score)


Fixed submission:
import numpy as np
from bisect import bisect_left

D = int(input())
C = np.array(list(map(int, input().split())))
S = np.array([list(map(int, input().split())) for d in range(D)])

L = np.empty(26, int)
L[:] = -1

X = [[-1] for _ in range(26)]
T = []

score = 0
for d in range(D):
    t = int(input())-1
    T.append(t)
    L[t] = d
    score += S[d, t] - np.dot(C, d-L)
    X[t].append(d)

for x in X:
    x.append(D)
    
M = int(input())
for _ in range(M):
    d, q = map(int, input().split())
    d-=1
    q-=1

    t = T[d]
    T[d] = q
    
    i = bisect_left(X[t], d)
    d_prev = X[t][i-1]
    d_next = X[t][i+1]
    
    dd = d-d_prev-1
    score += C[t]*(dd+1)*dd//2
    dd = d_next-d-1
    score += C[t]*(dd+1)*dd//2
    dd = d_next-d_prev-1
    score -= C[t]*(dd+1)*dd//2

    X[t].pop(i)
    
    i = bisect_left(X[q], d)
    d_prev = X[q][i-1]
    d_next = X[q][i]
    
    dd = d-d_prev-1
    score -= C[q]*(dd+1)*dd//2
    dd = d_next-d-1
    score -= C[q]*(dd+1)*dd//2
    dd = d_next-d_prev-1
    score += C[q]*(dd+1)*dd//2

    X[q].insert(i, d)

    score += S[d,q] - S[d,t]

    print(score)