Problem:


(Please read problem A first. The maximum score you can get by solving this problem C is 1, which will have almost no effect on your ranking.)

Beginner's Guide

"Local search" is a powerful method for finding a high-quality solution.
In this method, instead of constructing a solution from scratch, we try to find a better solution by slightly modifying the already found solution.
If the solution gets better, update it, and if it gets worse, restore it.
By repeating this process, the quality of the solution is gradually improved over time.
The pseudo-code is as follows.

solution = compute an initial solution (by random generation, or by applying other methods such as greedy)
while the remaining time > 0:
    slightly modify the solution (randomly)
    if the solution gets worse:
        restore the solution

For example, in this problem, we can use the following modification: pick the date d and contest type q at random and change the type of contest to be held on day d to q.
The pseudo-code is as follows.

t[1..D] = compute an initial solution (by random generation, or by applying other methods such as greedy)
while the remaining time > 0:
    pick d and q at random
    old = t[d] # Remember the original value so that we can restore it later
    t[d] = q
    if the solution gets worse:
        t[d] = old

The most important thing when using the local search method is the design of how to modify solutions.

If the amount of modification is too small, we will soon fall into a dead-end (local optimum) and, conversely, if the amount of modification is too large, the probability of finding an improving move becomes extremely small.

In order to increase the number of iterations, it is desirable to be able to quickly calculate the score after applying a modification.

In this problem C, we focus on the second point.
The score after the modification can, of course, be obtained by calculating the score from scratch.
However, by focusing on only the parts that have been modified, it may be possible to quickly compute the difference between the scores before and after the modification.
From another viewpoint, the impossibility of such a fast incremental calculation implies that a small modification to the solution affects a majority of the score calculation.
In such a case, we may need to redesign how to modify solutions, or there is a high possibility that the problem is not suitable for local search.
Let's implement fast incremental score computation.
It's time to demonstrate the skills of algorithms and data structures you have developed in ABC and ARC!

In this kind of contest, where the objective is to find a better solution instead of the optimal one, a bug in a program does not result in a wrong answer, which may delay the discovery of the bug.
For early detection of bugs, it is a good idea to unit test functions you implemented complicated routines.
For example, if you implement fast incremental score calculation, it is a good idea to test that the scores computed by the fast implementation match the scores computed from scratch, as we will do in this problem C.

Problem Statement

You will be given a contest schedule for D days and M queries of schedule modification.
In the i-th query, given integers d_i and q_i, change the type of contest to be held on day d_i to q_i, and then output the final satisfaction at the end of day D on the updated schedule.
Note that we do not revert each query. That is, the i-th query is applied to the new schedule obtained by the (i-1)-th query.

Input

Input is given from Standard Input in the form of the input of Problem A followed by the output of Problem A and the queries.

D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
M
d_1 q_1
d_2 q_2
\vdots
d_M q_M

The constraints and generation methods for the input part are the same as those for Problem A.

For each d=1,\ldots,D, t_d is an integer generated independently and uniformly at random from {1,2,\ldots,26}.

The number of queries M is an integer satisfying 1\leq M\leq 10^5.

For each i=1,\ldots,M, d_i is an integer generated independently and uniformly at random from {1,2,\ldots,D}.

For each i=1,\ldots,26, q_i is an integer satisfying 1\leq q_i\leq 26 generated uniformly at random from the 25 values that differ from the type of contest on day d_i.

Output

Let v_i be the final satisfaction at the end of day D on the schedule after applying the i-th query.
Print M integers v_i to Standard Output in the following format:

v_1
v_2
\vdots
v_M

Sample Input 1

5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192
1
17
13
14
13
5
1 7
4 11
3 4
5 24
4 19

Sample Output 1

72882
56634
38425
27930
42884

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case.

Next Step

Let's go back to Problem A and implement the local search algorithm by utilizing the incremental score calculator you just implemented!
For this problem, the current modification "pick the date d and contest type q at random and change the type of contest to be held on day d to q" is actually not so good. By considering why it is not good, let's improve the modification operation.
One of the most powerful and widely used variant of the local search method is "Simulated Annealing (SA)", which makes it easier to reach a better solution by stochastically accepting worsening moves.
For more information about SA and other local search techniques, please refer to the editorial that will be published after the contest.



Sample input:
5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192
1
17
13
14
13
5
1 7
4 11
3 4
5 24
4 19


Sample output:
72882
56634
38425
27930
42884

Submission status: Time Limit Exceeded

Solution code:
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')




def solve():
    D = ni()
    c = nl()
    s = [nl() for _ in range(D)]
    sche = [ni() - 1 for _ in range(D)]
    M = ni()

    def calc(m=26):
        last = [-1] * m
        ans = 0
        for i in range(D):
            t = sche[i]
            ans += s[i][t]
            last[t] = i
            for j in range(m):
                ans -= c[j] * (i - last[j])
        return ans

    def change(d, q, sc):
        b = sche[d]
        fq, bq = -1, D
        fb, bb = -1, D
        for i in range(d):
            if sche[i] == q:
                fq = i
            if sche[i] == b:
                fb = i
        for i in range(D-1, d, -1):
            if sche[i] == q:
                bq = i
            if sche[i] == b:
                bb = i
        sc += ((bb - d) * (bb - d - 1) // 2 + (d - fb) * (d - fb - 1) // 2) * c[b]
        sc += ((bq - fq) * (bq - fq - 1) // 2) * c[q]
        sc -= s[d][b]
        sc -= ((bq - d) * (bq - d - 1) // 2 + (d - fq) * (d - fq - 1) // 2) * c[q]
        sc -= ((bb - fb) * (bb - fb - 1) // 2) * c[b]
        sc += s[d][q]
        return sc

    sc = calc()

    for _ in range(M):
        d, q = nm()
        d -= 1; q -=1
        sc = change(d, q, sc)
        print(sc)
        sche[d] = q

    return

solve()

Submission status: Time Limit Exceeded

Solution code:
D = int(input())
C = [int(_) for _ in input().split()]
S = [[int(_) for _ in input().split()] for _ in range(D)]
T = [int(input()) - 1 for _ in range(D)]
M = int(input())
DQ = [[int(_) for _ in input().split()] for _ in range(M)]

for d, q in DQ:
    T[d - 1] = q - 1
    ans = 0
    last = [-1] * 26
    for day, t in enumerate(T):
        ans += S[day][t]
        last[t] = day
        for t2, day2 in enumerate(last):
            ans -= C[t2] * (day - day2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
d = int(input())
cs = list(map(int, input().split()))
sm = [list(map(int, input().split())) for _ in range(d)]

def total_satisfaction(ts):
    ls = [0] * 26
    s = 0
    for i in range(d):
        t = ts[i]
        t -= 1
        s += sm[i][t]
        ls[t] = i + 1
        dis = sum(c * ((i+1) - l) for c, l in zip(cs, ls))
        s -= dis
    return s

ts = [int(input()) for _ in range(d)]
m = int(input())
dqs = [list(map(int, input().split())) for _ in range(m)]
for di, qi in dqs:
    ts[di - 1] = qi
    s = total_satisfaction(ts)
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline


def get_sat():
    sat = 0
    when = [-1] * 26
    for i in range(d):
        sat += s[i][t[i]]
        when[t[i]] = i
        for j in range(26):
            sat -= c[j] * (i - when[j])
    return sat


d = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(d)]
t = [int(input()) - 1 for i in range(d)]
m = int(input())
for _ in range(m):
    p, q = map(int, input().split())
    t[p - 1] = q - 1
    print(get_sat())

Submission status: Time Limit Exceeded

Solution code:
from random import randint

D = int(input())
cl = list(map(int, input().split()))
sl = [list(map(int, input().split())) for _ in range(D)]
tl = [int(input()) for _ in range(D)]
M = int(input())
dq = [list(map(int, input().split())) for _ in range(M)]

def score(d, ti, last):
    s = sl[d-1][ti]
    for i in range(len(cl)):
        c = cl[i]
        s -= c*(d-last[i])
    return s

def scores(tl):
    s = 0
    last = [0]*26
    for d in range(D):
        t = tl[d] - 1
        last[t] = d+1
        s += score(d+1,t,last)
    return s


for i in range(M):
    d,q = dq[i]
    x = tl[d-1]
    tl[d-1] = q
    print(scores(tl))

Submission status: Time Limit Exceeded

Solution code:
D=int(input())
C=list(map(int,input().split()))
S=[list(map(int,input().split())) for i in range(D)]
T=[int(input())-1 for i in range(D)]
X=[0]*26
Y=[0]*26
M=int(input())
d,q=0,0
P=0
for i in range(D):
  P+=S[i][T[i]]
  for j in range(26):
    X[j]+=C[j]
  X[T[i]]=0
  for j in range(26):
    Y[j]+=X[j]
for i in range(M):
  d,q=map(int,input().split())
  d,q=d-1,q-1
  P-=S[d][T[d]]
  P+=S[d][q]
  for j in range(26):
    X[j]=0
  Y[T[d]],Y[q]=0,0
  for j in range(D):
    if j==d:
      Z=q
    else:
      Z=T[j]
    for j in range(26):
      X[j]+=C[j]
    X[Z]=0
    Y[T[d]]+=X[T[d]]
    Y[q]+=X[q]
  T[d]=q
  print(P-sum(Y))

Submission status: Time Limit Exceeded

Solution code:
d=int(input())
c=[0]+[int(j)for j in input().split()]
s=[[0]+[int(j)for j in input().split()]for i in range(d)]
t=[int(input())for i in range(d)]
m=int(input())
dq=[[int(j)for j in input().split()]for i in range(m)]
for p,q in dq:
    l=[0]*27
    ans=0
    t[p-1]=q
    for i,j in enumerate(t,1):
        ans+=s[i-1][j]
        l[j]=i
        for k in range(1,27):
            ans-=(i-l[k])*c[k]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

D=int(input())
C=[0]+list(map(int,input().split()))
S=[0]+[[0]+list(map(int,input().split())) for i in range(D)]
T=[0]+[int(input()) for i in range(D)]

ANS=0
LAST=[[0]*27 for i in range(D+1)]
for day in range(1,D+1):
    for j in range(1,27):
        LAST[day][j]=LAST[day-1][j]
    t=T[day]
    ANS+=S[day][t]
    LAST[day][t]=day

    for j in range(1,27):
        ANS-=C[j]*(day-LAST[day][j])

    #print(ANS)

M=int(input())
for tests in range(M):
    d,after_t=map(int,input().split())

    before_t=T[d]

    ANS+=S[d][after_t]-S[d][before_t]

    T[d]=after_t

    for day in range(d,D+1):
        if T[day]!=before_t:
            ANS+=C[before_t]*(day-LAST[day][before_t])
            LAST[day][before_t]=LAST[day-1][before_t]
            ANS-=C[before_t]*(day-LAST[day][before_t])
        else:
            break

    ANS+=C[after_t]*(d-LAST[d][after_t])
    LAST[d][after_t]=d

    for day in range(d+1,D+1):    
        if T[day]!=after_t:
            ANS+=C[after_t]*(day-LAST[day][after_t])
            LAST[day][after_t]=LAST[day-1][after_t]
            ANS-=C[after_t]*(day-LAST[day][after_t])

    print(ANS)

Submission status: Time Limit Exceeded

Solution code:
D=int(input())
c=list(map(int,input().split()))
s= [list(map(int,input().split())) for _ in range(D)]
t = [int(input()) for _ in  range(D)]
M=int(input())
d=[0]*M
q=[0]*M
for _ in range(M):
    d[_],q[_] = map(int,input().split())
    
#last = [0 for _ in range(26)]
#sati=0
#for i in range(D):
#    sati += s[i][t[i]-1]
#    last[t[i]-1]=i+1
#    for j in range(26):
#        sati -= c[j]*(i+1-last[j])
#print(sati)
#print(t)

for k in range(M):
    sati=0
    last = [0 for _ in range(26)]
    t[d[k]-1] = q[k]
    for i in range(D):
        sati += s[i][t[i]-1]
        last[t[i]-1]=i+1
        for j in range(26):
            sati -= c[j]*(i+1-last[j])
    print(sati)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline

def calc(d,C,S,T):
    Last=[0]*26
    score=0
    for day,t in enumerate(T):
        t-=1
        Last[t]=day+1
        score+=S[day][t]
        for i,c in enumerate(C):
            score-=c*(day+1-Last[i])
    return score

def main():
    d=int(input())
    C=tuple(map(int,input().split()))
    S=[tuple(map(int,input().split())) for _ in range(d)]
    T=[int(input()) for _ in range(d)]
    #print(calc(d,C,S,T))
    m=int(input())
    for _ in range(m):
        d,q=map(int,input().split())
        T[d-1]=q
        score=calc(d,C,S,T)
        print(score)
    
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    read = sys.stdin.buffer.read
    readline = sys.stdin.buffer.readline
    readlines = sys.stdin.buffer.readlines
    sys.setrecursionlimit(10 ** 7)

    def score(ans):
        memo = [0] * 26
        for i, tt in enumerate(t):
            memo[tt - 1] = i + 1
            for m, cc in zip(memo, c):
                ans -= cc * (i + 1 - m)
        return ans

    d = int(readline())
    c = list(map(int, readline().split()))
    s = [list(map(int, readline().split())) for _ in range(d)]
    t = [int(readline()) for _ in range(d)]
    v = 0
    for i, tt in enumerate(t):
        v += s[i][tt - 1]
    m = int(readline())
    for _ in range(m):
        d, q = map(int, readline().split())
        d -= 1
        v -= s[d][t[d] - 1]
        t[d] = q
        v += s[d][t[d] - 1]
        print(score(v))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque, Counter, defaultdict
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math
import bisect
from functools import lru_cache
import itertools

sys.setrecursionlimit(10 ** 8)

M = 10 ** 9 + 7
INF = 10 ** 17


def main():
    D = int(input())
    c = [int(a) for a in input().split()]
    s = [
        [int(a) for a in input().split()]
        for _ in range(D)
    ]
    t = [
        int(input())
        for _ in range(D)
    ]
    M = int(input())
    dq = [
        [int(a) for a in input().split()]
        for _ in range(M)
    ]

    def calc_score(t):
        score = 0
        last = [0] * len(c)
        res = []

        for d, ti in enumerate(t):
            sdi = s[d][ti - 1]
            score += sdi
            last[ti - 1] = d + 1
            for i, ci in enumerate(c):
                score -= ci * ((d + 1) - last[i])
            res.append(score)
        return res

    for di, qi in dq:
        t[di - 1] = qi
        scores = calc_score(t)
        print(scores[-1])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
D=int(input())
*c,=map(int,input().split())
s=[]
for i in range(D):
    *x,=map(int,input().split())
    s.append(x)
T=[]
for i in range(D):
    T.append(int(input()))

def score(T):
    last=[-1]*26
    v=0
    for d in range(D):
        v+=s[d][T[d]-1]
        last[T[d]-1]=d
        for i in range(26):
            v-=c[i]*(d-last[i])
    return v

m=int(input())
for i in range(m):
    d,q=map(int,input().split())
    T[d-1]=q
    print(score(T))

Submission status: Time Limit Exceeded

Solution code:
d=int(input())
c=list(map(int,input().split()))
s=[]
for i in range(d):
	r=list(map(int,input().split()))
	s.append(r)

def score(T):
	last=[0]*26
	happy=0
	for i in range(d):
		last[T[i]-1]=i+1
		happy+=s[i][T[i]-1]
		for j in range(26):
			happy-=(i+1-last[j])*c[j]
	return happy

t=[int(input())for _ in range(d)]
m=int(input())
for i in range(m):
	D,q=map(int,input().split())
	t[D-1]=q
	print(score(t))

Submission status: Time Limit Exceeded

Solution code:
D=int(input())
c=list(map(int,input().split()))
s= [list(map(int,input().split())) for _ in range(D)]
t = [int(input()) for _ in  range(D)]
M=int(input())
d=[0]*M
q=[0]*M
for _ in range(M):
    d[_],q[_] = map(int,input().split())
    
last = [0 for _ in range(26)]
sati=0
for i in range(D):
    sati += s[i][t[i]-1]
    last[t[i]-1]=i+1
    for j in range(26):
        sati -= c[j]*(i+1-last[j])
#print(sati)
#print(t)

for k in range(M):
    sati=0
    last = [0 for _ in range(26)]
    t[d[k]-1] = q[k]
    for i in range(D):
        sati += s[i][t[i]-1]
        last[t[i]-1]=i+1
        for j in range(26):
            sati -= c[j]*(i+1-last[j])
    print(sati)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
D=int(input())
clist=list(map(int,input().split()))
smat=[[int(x) for x in input().split()] for _ in range(D)]
#print(clist)
#print(smat)

tlist=[]
for d in range(D):
  t=int(input())
  tlist.append(t)
  
M=int(input())
for i in range(M):
  d,q=map(int,input().split())
  tlist[d-1]=q

  last_dic=defaultdict(int)
  answer=0
  for d in range(D):
    #t=int(input())
    t=tlist[d]
    answer+=smat[d][t-1]
    last_dic[t]=d+1

    for i in range(26):
      answer-=clist[i]*(d+1-last_dic[i+1])
    
  print(answer)

Submission status: Time Limit Exceeded

Solution code:
D=int(input())
c=list(map(int,input().split()))
s=[list(map(int,input().split())) for i in [0]*D]
t=[int(input()) for i in [0]*D]
m=int(input())
r=[list(map(int,input().split()))for i in [0]*m]
for a in r:
    t[a[0]-1]=a[1]
    k=0
    n=1
    p=[0]*26
    for i in t:
        k+=s[n-1][i-1]
        p[i-1]=n
        k-=sum([(n-p[i])*c[i] for i in range(26)])
        n+=1
    print(k)

Submission status: Time Limit Exceeded

Solution code:
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]
T = [int(input()) for _ in range(D)]
m = int(input())
for _ in range(m):
    d, q = map(int, input().split())
    T[d-1] = q
    last = [-1]*26
    ans = 0
    for d in range(D):
        t = T[d]-1
        last[t] = d
        for i in range(26):
            ans -= (d-last[i])*c[i]
        ans += s[d][t]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest
import copy

def getScore(d,c,s,t):
    last=[-1]*26
    score=0
    for i in range(d):
        last[t[i]]=i
        score+=s[i][t[i]]
        for j in range(26):
            score-=(i-last[j])*c[j]
    return score

def resolve():
    readline=sys.stdin.readline

    d=int(readline())
    c=list(map(int, readline().rstrip().split()))
    s=[0]*d
    for i in range(d):
        s[i]=list(map(int, readline().rstrip().split()))
    last=[-1]*26
    t=[0]*d
    for i in range(d):
        t[i]=int(readline())-1
    m=int(readline())
    for i in range(m):
        dm,qm=map(int, readline().rstrip().split())
        dm-=1
        qm-=1
        #t2=copy.copy(t)
        t[dm]=qm
        print(getScore(d,c,s,t))
    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    read = sys.stdin.buffer.read
    readline = sys.stdin.buffer.readline
    readlines = sys.stdin.buffer.readlines
    sys.setrecursionlimit(10 ** 7)

    def score(t):
        ans = 0
        memo = [0] * 26
        for i, tt in enumerate(t):
            ans += s[i][tt - 1]
            memo[tt - 1] = i + 1
            for m, cc in zip(memo, c):
                ans -= cc * (i + 1 - m)
        return ans

    d = int(readline())
    c = list(map(int, readline().split()))
    s = [list(map(int, readline().split())) for _ in range(d)]
    t = [int(readline()) for _ in range(d)]
    m = int(readline())
    for _ in range(m):
        d, q = map(int, readline().split())
        t[d - 1] = q
        print(score(t))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
D = int(input())
C = list(map(int, input().split()))

S = []
for _ in range(D):
  S.append(list(map(int, input().split())))

T = []
for _ in range(D):
  T.append(int(input())-1)

M = int(input())
Q = []
for _ in range(M):
  Q.append(list(map(int, input().split())))

def solve():
  LastD = [0] * 26
  sc = 0
  for i in range(D):
    t = T[i]
    sc += S[i][t]
    LastD[t] = i+1
    for j in range(26):
      sc -= C[j] * (i+1 - LastD[j])
  return sc
    

for d,q in Q:
  T[d-1] = q-1
  print(solve())

Submission status: Time Limit Exceeded

Solution code:
d = int(input())
c_li = list(map(int, input().split()))
s_li = [list(map(int, input().split())) for _ in range(d)]
t_li = [int(input()) for _ in range(d)]
m = int(input())
m_li = [list(map(int, input().split())) for _ in range(m)]


for k in range(m):
    ans = 0
    last_d = [-1] * 26
    t_li[m_li[k][0]-1] = m_li[k][1]
    for i in range(d):
        op = t_li[i] - 1
        ans += s_li[i][op]
        last_d[op] = i
        for j in range(26):
            ans -= c_li[j] * (i-last_d[j])
        
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    D = int(input())
    c = list(map(int, input().split()))
    s = [list(map(int, input().split())) for _ in [0]*D]
    ans = [int(input()) for _ in [0]*D]
    M = int(input())
    dq = [list(map(int, input().split())) for _ in [0]*M]

    def calc_score(ans):
        score = 0
        last = [-1]*26
        for d in range(D):
            ct = ans[d]-1  # 開くコンテスト
            score += s[d][ct]
            last[ct] = d
            for j in range(26):
                score -= c[j]*(d-last[j])
        print(score)

    for d, q in dq:
        ans[d-1] = q
        calc_score(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for _ in range(D)]
T = [int(input()) for _ in range(D)]

M = int(input())
Q = [tuple(map(int, input().split())) for _ in range(M)]



for i in range(M):
    res = 0
    last = [0 for _ in range(26)]
    T[Q[i][0] - 1] = Q[i][1]
    for d in range(D):
        res += S[d][T[d] - 1]
        last[T[d] - 1] = d + 1
        for j in range(26):
            res -= C[j] * ((d + 1) - last[j])
    print(res)

Submission status: Time Limit Exceeded

Solution code:
D = int(input())
c = [int(x) for x in input().split()]
s = [[int(x) for x in input().split()] for _ in range(D)]

T = [int(input())-1 for _ in range(D)]

M = int(input())

S = []
for i in range(M):
    d,q = map(int,input().split())
    T[d-1] = q-1
    last = [-1 for _ in range(26)]
    satisfy = 0
    for day, t in enumerate(T):
        last[t] = day
        loss = sum(c[i]*(day-last[i]) for i in range(26))
        satisfy += s[day][t] - loss
    S.append(satisfy)

for x in S:
    print(x)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
def calc(d,C,S,T):
    Last=[0]*26
    score=0
    for day,t in enumerate(T):
        t-=1
        Last[t]=day+1
        score+=S[day][t]
        for i,c in enumerate(C):
            score-=c*(day+1-Last[i])
    return score

def main():
    d=int(input())
    C=tuple(map(int,input().split()))
    S=[list(map(int,input().split())) for _ in range(d)]
    T=[int(input()) for _ in range(d)]
    #print(calc(d,C,S,T))
    m=int(input())
    Q=[list(map(int,input().split())) for _ in range(m)]
    for d,q in Q:
        T[d-1]=q
        score=calc(d,C,S,T)
        print(score)
    
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

D=int(input())
c=np.array(list(map(int,input().split())))
s=[list(map(int,input().split())) for _ in range(D)]
t=[int(input()) for _ in range(D)]
M=int(input())
DQ=[list(map(int,input().split())) for _ in range(M)]

for i in range(M):
    d=DQ[i][0]
    q=DQ[i][1]
    t[d-1]=q
    last=np.array([0]*26)
    v=0
    for d in range(1,D+1):
        v+=s[d-1][t[d-1]-1]
        last[t[d-1]-1]=d
        v-=np.dot(c,d-last)
    print(v)

Submission status: Time Limit Exceeded

Solution code:
D = int(input())
c = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(D)]
t = [int(input()) for i in range(D)]
M = int(input())
Q = [list(map(int,input().split())) for i in range(M)]

def content():
    cont = 0
    last = [0]*26
    for i in range(D):
        cont += s[i][t[i]-1]
        last[t[i]-1] = i+1
        for j in range(26):
            cont -= c[j]*(i+1-last[j])
    return cont

for i in range(M):
    d,q = Q[i]
    t[d-1] = q
    print(content())

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin, setrecursionlimit
setrecursionlimit(100000)

def compute_score(D, C, S, sol):
    last = [0] * 26
    score = 0
    lscores = []
    for i in range(D):
        j = sol[i] - 1
        last[j] = i + 1
        for k in range(26):
            score -= C[k] * (i + 1 - last[k])
        score += S[i][j]
        lscores.append(score)
    return lscores[-1]


def main():
    from builtins import int, map
    D = int(input())
    C = list(map(int, input().split()))
    S = [list(map(int, input().split())) for _ in range(D)]

    sol_init = [int(input()) for _ in range(D)]
    # print(compute_score(D, C, S, sol_init))

    # 解の変更を読み込む
    M = int(input())
    sol = sol_init
    for m in range(M):
        di, qi = map(int, input().split())
        sol[di - 1] = qi
        sol_v = compute_score(D, C, S, sol)
        # print(m, di, qi, sol, sol_v)
        print(sol_v)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def test():
    m=0
    for i in range(D):
        m+=s[i][t[i]-1]
        last[t[i]-1]=i+1
        for j in range(26):
            m-=c[j]*(i+1-last[j])
    print(m)
    return

D=int(input())
c=list(map(int,input().split()))
s=[list(map(int,input().split())) for i in range(D)]
t=[int(input()) for i in range(D)]
M=int(input())
for i in range(M):
    last=[0]*26
    d,q=map(int,input().split())
    t[d-1]=q
    test()

Submission status: Time Limit Exceeded

Solution code:
def main():
    D = int(input())
    c = list(map(int, input().split()))
    s = [list(map(int, input().split())) for _ in [0]*D]
    ans = [int(input()) for _ in [0]*D]
    M = int(input())
    dq = [list(map(int, input().split())) for _ in [0]*M]

    def calc_score(ans):
        score = 0
        last = [-1]*26
        for d in range(D):
            ct = ans[d]-1  # 開くコンテスト
            score += s[d][ct]
            last[ct] = d
            for j in range(26):
                score -= c[j]*(d-last[j])
        print(score)

    for d, q in dq:
        ans[d-1] = q
        calc_score(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]
t = [0] * D
for i in range(D): t[i] = int(input())

last = [0] * 26
def score(day, score):
  for i in range(26): score -= (day - last[i]) * c[i]
  return score
def run():
  global last, t
  last = [0] * 26
  res = 0
  for d in range(D):
    contest = t[d] - 1
    last[contest] = d + 1
    res = score(d + 1, res) + s[d][contest]
  return res
def change(day, contest):
  global t
  t[day - 1] = contest
  return run()
M = int(input())
for _ in range(M):
  d, q = map(int, input().split())
  print(change(d, q))

Submission status: Time Limit Exceeded

Solution code:
def scoring(T):
    score = 0
    last = [0] * 26
    for d, t in enumerate(T, 1):
        score += S[d - 1][t - 1]
        last[t - 1] = d
        for c, l in zip(C, last): score -= c * (d - l)
    return score

D = int(input())
*C, = map(int, input().split())
S = [list(map(int, input().split())) for _ in range(D)]
T = [int(input()) for _ in range(D)]
M = int(input())
for _ in range(M):
    d, q = map(int, input().split())
    T[d - 1] = q
    print(scoring(T))

Submission status: Time Limit Exceeded

Solution code:
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def calc(D, c, s, sche, m=26):
    last = [-1] * m
    ans = 0
    for i in range(D):
        t = sche[i]
        ans += s[i][t]
        last[t] = i
        for j in range(m):
            ans -= c[j] * (i - last[j])
    return ans

def solve():
    D = ni()
    c = nl()
    s = [nl() for _ in range(D)]
    sche = [ni() - 1 for _ in range(D)]
    M = ni()
    for _ in range(M):
        d, q = nm()
        d -= 1; q -=1
        sche[d] = q
        print(calc(D, c, s, sche))

    return

solve()