Buggy python submission (verdict: Time Limit Exceeded):
_,C,*I=[[*map(int,t.split())]for t in open(0)]
for d,q in I[731:]:
 I[d+364][0]=q;i,R,P,*L=[0]*29
 while i<365:t=I[i+365][0]-1;P+=sum(C)+(~i+L[t])*C[t];R+=I[i][t]-P;i+=1;L[t]=i
 print(R)

Fixed submission:
def P(s):
  L = [0] * 26
  R = P = 0
  for i in range(D):
    t = I[i + D][0] - 1
    R += I[i][t]
    P += s - (i + 1 - L[t]) * C[t]
    R -= P
    L[t] = i + 1
  return R
(D,), C, *I = [list(map(int, o.split())) for o in open(0)]
for d, q in I[2*D + 1:]:
  I[d - 1 + D][0] = q
  print(P(sum(C)))

Buggy python submission (verdict: Time Limit Exceeded):
(D,),C,*I=[[*map(int,t.split())]for t in open(0)]
for d,q in I[D-~D:]:
 I[d-1+D][0]=q;i,R,P,*L=[0]*29
 while i<D:t=I[i+D][0]-1;P+=sum(C)+(~i+L[t])*C[t];R+=I[i][t]-P;i+=1;L[t]=i
 print(R)

Fixed submission:
def P(s):
  L = [0] * 26
  R = P = 0
  for i in range(D):
    t = I[i + D][0] - 1
    R += I[i][t]
    P += s - (i + 1 - L[t]) * C[t]
    R -= P
    L[t] = i + 1
  return R
(D,), C, *I = [list(map(int, o.split())) for o in open(0)]
for d, q in I[2*D + 1:]:
  I[d - 1 + D][0] = q
  print(P(sum(C)))

Buggy python submission (verdict: Runtime Error):
def P(s):
 R,P,*L=[0]*28
 for i in range(1,366):t=I[i+364][0]-1;P+=s-(i-L[t])*C[t];R+=I[i-1][t]-P;L[t]=i
 return R
_,C,*I=[[*map(int,t.split())]for t in open(0)]
for d,q in I[701:]:I[d+364][0]=q;print(P(sum(C)))

Fixed submission:
def P(s):
  L = [0] * 26
  R = P = 0
  for i in range(D):
    t = I[i + D][0] - 1
    R += I[i][t]
    P += s - (i + 1 - L[t]) * C[t]
    R -= P
    L[t] = i + 1
  return R
(D,), C, *I = [list(map(int, o.split())) for o in open(0)]
for d, q in I[2*D + 1:]:
  I[d - 1 + D][0] = q
  print(P(sum(C)))

Buggy python submission (verdict: Runtime Error):
def P(s):
 R,P,*L=[0]*28
 for i in range(365):t=I[i+365][0]-1;P+=s-(i+1-L[t])*C[t];R+=I[i][t]-P;L[t]=i+1
 return R
_,C,*I=[[*map(int,t.split())]for t in open(0)]
for d,q in I[701:]:I[d+364][0]=q;print(P(sum(C)))

Fixed submission:
def P(s):
  L = [0] * 26
  R = P = 0
  for i in range(D):
    t = I[i + D][0] - 1
    R += I[i][t]
    P += s - (i + 1 - L[t]) * C[t]
    R -= P
    L[t] = i + 1
  return R
(D,), C, *I = [list(map(int, o.split())) for o in open(0)]
for d, q in I[2*D + 1:]:
  I[d - 1 + D][0] = q
  print(P(sum(C)))

Buggy python submission (verdict: Time Limit Exceeded):
_,C,*I=[list(map(int,o.split()))for o in open(0)]
for d,q in I[731:]:
 I[d+364][0]=q;L=[0]*26;R=P=0
 for i in range(365):t=I[i+365][0]-1;P+=sum(C)-(i+1-L[t])*C[t];R+=I[i][t]-P;L[t]=i+1
 print(R)

Fixed submission:
def P(s):
  L = [0] * 26
  R = P = 0
  for i in range(D):
    t = I[i + D][0] - 1
    R += I[i][t]
    P += s - (i + 1 - L[t]) * C[t]
    R -= P
    L[t] = i + 1
  return R
(D,), C, *I = [list(map(int, o.split())) for o in open(0)]
for d, q in I[2*D + 1:]:
  I[d - 1 + D][0] = q
  print(P(sum(C)))

Buggy python submission (verdict: Time Limit Exceeded):
def P(s):
  L = [0] * 26
  R = P = 0
  for i in range(D):
    t = I[i + D][0] - 1
    R += I[i][t]
    P += s - (i + 1 - L[t]) * C[t]
    R -= P
    L[t] = i + 1
  return R
(D,), C, *I = [list(map(int, o.split())) for o in open(0)]
for d, q in I[2*D + 1:]:
  I[d - 1 + D][0] = q
  print(P(sum(C)))

Fixed submission:
def P(s):
  L = [0] * 26
  R = P = 0
  for i in range(D):
    t = I[i + D][0] - 1
    R += I[i][t]
    P += s - (i + 1 - L[t]) * C[t]
    R -= P
    L[t] = i + 1
  return R
(D,), C, *I = [list(map(int, o.split())) for o in open(0)]
for d, q in I[2*D + 1:]:
  I[d - 1 + D][0] = q
  print(P(sum(C)))

Buggy python submission (verdict: Time Limit Exceeded):
import time
start = time.time()
import random
D = int(input())
*c, = map(int, input().split())
S = []
for _ in [None] * D:
    *s, = map(int, input().split())
    S += s,
INF = float("inf")

def Tread(D):
    return [int(input()) - 1 for i in [None] * D]

def check(T):
    M = 0
    MM = []
    last = [-1] * 26
    for d, t in enumerate(T):
        M += S[d][t] - sum((d - last[i]) * c[i] for i in range(26) if i != t)
        last[t] = d
        # MM += M,
    return M # MM

def C():
    T = Tread(D)
    for i in range(int(input())):
        d, q = map(int, input().split())
        T[d - 1] = q - 1
        print(check(T))
C()

Fixed submission:
def P(s):
  L = [0] * 26
  R = P = 0
  for i in range(D):
    t = I[i + D][0] - 1
    R += I[i][t]
    P += s - (i + 1 - L[t]) * C[t]
    R -= P
    L[t] = i + 1
  return R
(D,), C, *I = [list(map(int, o.split())) for o in open(0)]
for d, q in I[2*D + 1:]:
  I[d - 1 + D][0] = q
  print(P(sum(C)))