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:
from numba import njit
import numpy as np
d = int(input())
cs = list(map(int, input().split()))
sm = [list(map(int, input().split())) for _ in range(d)]

cs = np.array(cs, dtype=np.int64)
sm = np.array(sm, dtype=np.int64)

@njit('i8(i8[:])', cache=True)
def total_satisfaction(ts):
    ls = np.zeros(26, dtype=np.int64)
    s = 0
    for i in range(d):
        t = ts[i]
        t -= 1
        s += sm[i][t]
        ls[t] = i + 1

        dv = cs * ((i+1) - ls)
        s -= dv.sum()
    return s

ts = [int(input()) for _ in range(d)]
ts = np.array(ts, dtype=np.int64)
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


def B(T):
    global S, Z, D
    _last = [0] * Z
    cnt = 0
    for d in range(1, D + 1):
        t = T[d]
        _last[t] = d
        up = S[d][t]
        down = sum(C[i] * (d - _last[i]) for i in range(Z))
        cnt += up - down
    return cnt


def checkScore(r):
    global D
    L = last[r]
    rst = 0
    for d in range(1, D + 1):
        down = C[r] * (d - L[d])
        up = S[d][r] if down == 0 else 0
        rst += up - down
    return rst


Z = 26
D = int(input())
C = list(map(int, input().split()))
S = [tuple([0] * Z)]
S += [tuple(map(int, input().split())) for _ in range(D)]
T = [None] + [int(input()) - 1 for _ in range(D)]

M = int(input())
Q = [tuple(map(lambda x: int(x) - 1, s.split())) for s in sys.stdin.readlines()]


last = [[0] * (D + 1) for _ in range(Z)]
V = [0] * Z
for d in range(1, D + 1):
    t = T[d]
    for p in range(Z):
        last[p][d] = last[p][d - 1]
    last[t][d] = d
for p in range(Z):
    V[p] = checkScore(p)

score = sum(V)

for d, q in Q:
    d += 1

    p = T[d]
    T[d] = q

    for r, L in ((p, last[p]), (q, last[q])):
        for e in range(1, D + 1):
            L[e] = L[e - 1] if r != T[e] else e

    score -= V[p] + V[q]
    V[p] = checkScore(p)
    V[q] = checkScore(q)
    score += V[p] + V[q]
    # print(score == B(T), score, B(T))
    print(B(T))

Submission status: Time Limit Exceeded

Solution code:
#d=日数, c=満足低下度, s=満足上昇度, t=コンテストの日程 
d = int(input())
c = list(map(int, input().split()))
#print(c)
s = [list(map(int, input().split())) for i in range(d)]
t = [int(input()) for j in range(d)]
m = int(input())
#dq = [list(map(int, input().split())) for i in range(m)]
for j in range(m):
  dq = list(map(int, input().split()))
  res = 0
  last = [0 for i in range(26)]
  #old = t[dq[j][0] - 1]
  t[dq[0] - 1] = dq[1]
  for day in range(d):
    res += s[day][t[day]-1]
    last[t[day]-1] = day+1
    #for i in range(26):
      #res -= c[i] * (day + 1 - last[i])
    combined1 = [x * (day + 1 - y) for (x, y) in zip(c, last)]
    res -= sum(combined1)
  #t[dq[j][0] - 1] = old
  print(res)

Submission status: Time Limit Exceeded

Solution code:
from numba import njit
import numpy as np
d = int(input())
cs = list(map(int, input().split()))
sm = [list(map(int, input().split())) for _ in range(d)]

cs = np.array(cs, dtype=np.int64)
sm = np.array(sm, dtype=np.int64)

@njit('i8(i8[:])', cache=True)
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 = 0
        for j in range(26):
            dis += cs[j] * ((i+1) - ls[j])
        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
    v = np.array(ts, dtype=np.int64)
    s = total_satisfaction(v)
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
#d=日数, c=満足低下度, s=満足上昇度, t=コンテストの日程 
d = int(input())
c = np.array(list(map(int, input().split())))
#print(c)
s = [list(map(int, input().split())) for i in range(d)]
t = [int(input()) for j in range(d)]
m = int(input())
#dq = [list(map(int, input().split())) for i in range(m)]
for j in range(m):
  dq = list(map(int, input().split()))
  res = 0
  last = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
  #old = t[dq[j][0] - 1]
  t[dq[0] - 1] = dq[1]
  for day in range(d):
    res += s[day][t[day]-1]
    last[t[day]-1] = day+1
    #for i in range(26):
      #res -= c[i] * (day + 1 - last[i])
    #combined1 = [x * (day + 1 - y) for (x, y) in zip(c, last)]
    #res -= sum(combined1)
    la = np.array([(day + 1 - y) for y in last])
    res -= np.sum(c*la)
  #t[dq[j][0] - 1] = old
  print(res)

Submission status: Time Limit Exceeded

Solution code:
#d=日数, c=満足低下度, s=満足上昇度, t=コンテストの日程 
d = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for i in range(d)]
t = [int(input()) for j in range(d)]
m = int(input())
#dq = [list(map(int, input().split())) for i in range(m)]
for j in range(m):
  dq = list(map(int, input().split()))
  res = 0
  last = [0 for i in range(26)]
  #old = t[dq[j][0] - 1]
  t[dq[0] - 1] = dq[1]
  for day in range(d):
    res += s[day][t[day]-1]
    last[t[day]-1] = day+1
    for i in range(26):
      res -= c[i] * (day + 1 - last[i])
    #combined1 = [x * (day + 1 - y) for (x, y) in zip(c, last)]
    #res -= sum(combined1)
  #t[dq[j][0] - 1] = old
  print(res)

Submission status: Time Limit Exceeded

Solution code:
#d=日数, c=満足低下度, s=満足上昇度, t=コンテストの日程 
d = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for i in range(d)]
t = [int(input()) for j in range(d)]
m = int(input())
#dq = [list(map(int, input().split())) for i in range(m)]
for j in range(m):
  dq = list(map(int, input().split()))
  res = 0
  last = [0 for i in range(26)]
  #old = t[dq[j][0] - 1]
  t[dq[0] - 1] = dq[1]
  for day in range(d):
    res += s[day][t[day]-1]
    last[t[day]-1] = day+1
    #for i in range(26):
      #res -= c[i] * (day + 1 - last[i])
    combined1 = [x * (day + 1 - y) for (x, y) in zip(c, last)]
    res -= sum(combined1)
  #t[dq[j][0] - 1] = old
  print(res)

Submission status: Time Limit Exceeded

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

def last(p, k):
    x = bisect.bisect_right(last_hol[k], p,)
    if x == 0:
        return 0
    else:
        return last_hol[k][x - 1]


def score(t_in):
    ans = 0
    for i in range(d):
        ti = t_in[i]
        ans += s[i][ti - 1]
        for j in range(1, 27):
            ans -= c[j - 1] * (i + 1 - last(i + 1, j))
    return ans


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)]
last_hol = {i: [] for i in range(1, 27)}
for i in range(d):
    ti = t[i]
    last_hol[ti].append(i + 1)
v = []
for i in range(int(input())):
    a, b = map(int, input().split())
    t[a - 1] = b
    last_hol = {i: [] for i in range(1, 27)}
    for i in range(d):
        ti = t[i]
        last_hol[ti].append(i + 1)
    v.append(score(t))

for vi in v:
    print(vi)

Submission status: Time Limit Exceeded

Solution code:
#d=日数, c=満足低下度, s=満足上昇度, t=コンテストの日程 
d = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for i in range(d)]
t = [int(input()) for j in range(d)]
m = int(input())
dq = [list(map(int, input().split())) for i in range(m)]
for j in range(m):
  res = 0
  last = [0 for i in range(26)]
  #old = t[dq[j][0] - 1]
  t[dq[j][0] - 1] = dq[j][1]
  for day in range(d):
    res += s[day][t[day]-1]
    last[t[day]-1] = day+1
    #for i in range(26):
      #res -= c[i] * (day + 1 - last[i])
    combined1 = [x * (day + 1 - y) for (x, y) in zip(c, last)]
    res -= sum(combined1)
  #t[dq[j][0] - 1] = old
  print(res)

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 = [0] * D
for i in range(D):
    t[i] = int(input())
M = int(input())   
day = [0] * M
q = [0] * M
for i in range(M):
    day[i], q[i] = map(int, input().split())
    
    
m=[0]*D

a=[[0 for i in range(26)] for j in range(D)]
for u in range(D):
    i=t[u]
    for y in range(u,D):
        a[y][i-1]=u+1
    if u==0:
        m[u]=s[u][i-1]
    else:
        m[u]=m[u-1]+s[u][i-1]
mm=[0]*D
for d  in range(D):
    for i in range(26):
        mm[d]+=c[i]*(d+1-a[d][i])
    if not d==0:
        mm[d]+=mm[d-1]
for d in range(D):
    m[d]-=mm[d]
mmm=m[D-1]
#print(mmm)
#for i in a:
 #   print(i)
for p in range(M):
    new_type=q[p]
    old_type=t[day[p]-1]
    A=0
    B=0
    C=s[day[p]-1][old_type-1]-s[day[p]-1][new_type-1]
    for d in range(D):
        A+=c[new_type-1]*(d+1-a[d][new_type-1])+c[old_type-1]*(d+1-a[d][old_type-1])
    xday=day[p]
    if xday>1:
        before_new=a[xday-2][new_type-1]
        before_old=a[xday-2][old_type-1]
        now_new=a[day[p]-1][new_type-1]
        now_old=a[day[p]-1][old_type-1]
        for d in range(day[p]-1,D):
            if a[d][new_type-1]==now_new:
                a[d][new_type-1]=xday
            if a[d][old_type-1]==now_old:
                a[d][old_type-1]=before_old
    else:
        for d in range(day[p]-1,D):
            if a[d][old_type-1]==1:
                a[d][old_type-1]=0
            if a[d][new_type-1]==0:
                a[d][new_type-1]=1
 
        #a[d][new_type-1]+=1
        #a[d][old_type-1]-=1
    
    t[day[p]-1]=new_type
    for d in range(D):
        B+=c[new_type-1]*(d+1-a[d][new_type-1])+c[old_type-1]*(d+1-a[d][old_type-1])
    #for i in a:
     #   print(i,A,B,C)
    mmm=mmm-(B-A+C)
    print(mmm)

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 = []
q = []
for i in range(M):
    a, b = map(int, input().split())
    d.append(a)
    q.append(b)

    
now = 1
score = 0

decrease = 0


for i,z in enumerate(t):
    score += s[i][z - 1]

def change():
    ret = 0
    contest = [0] * 26
    date = [D * (1 + D) // 2] * 26
    for i, z in enumerate(t):
        date[z - 1] -= (D - i) * (i -contest[z-1]+ 1)
        contest[z - 1] = i + 1
    for i, z in enumerate(date):
        ret += c[i] * z
    return ret


for i, z in zip(d, q):
    score -= s[i - 1][t[i - 1] - 1]
    score += s[i - 1][z - 1]
    t[i - 1] = z
    decrease = change()
    print(score-decrease)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import random


def last(p, k):
    x = bisect.bisect_right(last_hol[k], p,)
    if x == 0:
        return 0
    else:
        return last_hol[k][x - 1]


def score(t_in):
    ans = 0
    for i in range(d):
        ti = t_in[i]
        ans += s[i][ti - 1]
        for j in range(1, 27):
            ans -= c[j - 1] * (i + 1 - last(i + 1, j))
    return ans


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)]
last_hol = {i: [] for i in range(1, 27)}
for i in range(d):
    ti = t[i]
    last_hol[ti].append(i + 1)
v = []
for i in range(int(input())):
    a, b = map(int, input().split())
    t[a - 1] = b
    last_hol = {i: [] for i in range(1, 27)}
    for i in range(d):
        ti = t[i]
        last_hol[ti].append(i + 1)
    v.append(score(t))
for vi in v:
    print(vi)

Submission status: Time Limit Exceeded

Solution code:
#d=日数, c=満足低下度, s=満足上昇度, t=コンテストの日程 
d = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for i in range(d)]
t = [int(input()) for j in range(d)]
m = int(input())
dq = [list(map(int, input().split())) for i in range(m)]
#last = [0 for i in range(26)]
#print(c)
#res = 0

#while count != len(t):
for j in range(m):
  res = 0
  last = [0 for i in range(26)]
  #old = t[dq[j][0] - 1]
  #print(t)
  t[dq[j][0] - 1] = dq[j][1]
  #print(t)
  #print(t[dq[j][0] - 1])
  for day in range(d):
    res += s[day][t[day]-1]
    last[t[day]-1] = day+1
    #print(last)
    #print(c[last[t[day]-1]])
    for i in range(26):
      #print(c[i] * (day + 1 - last[i]))
      res -= c[i] * (day + 1 - last[i])
  #t[dq[j][0] - 1] = old
  #print(t[dq[j][0] - 1])
  print(res)

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)]

tL = []
for i in range(D):
    tL.append(int(input()))

def cal():
    score = 0
    his = [0] * len(C)
    for i in range(D):
        t = tL[i]
        t -= 1
        his[t] = i + 1
        sat = 0
        for j in range(len(C)):
            sat += C[j] * (i - his[j] + 1)
        score += S[i][t] - sat
    return score

def search():
    M = int(input())
    score = []
    for _ in range(M):
        d, q = map(int, input().split())
        tL[d - 1] = q
        score.append(cal())
    return score

for s in search():
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from numba import jit
@jit(cache = True)
def main():
    d=int(input())
    c=list(map(int,input().split()))
    s=[list(map(int,input().split())) for _ in range(d)]
    last=[[0]*27 for _ in range(366)]
    srate=[0]
    T=[0]
    a=0
    for i in range(1,d+1):
        for j in range(27):
            last[i][j]=last[i-1][j]
        t=int(input())
        T.append(t)
        last[i][t]=i
        a+=s[i-1][t-1]
        for j in range(1,27):
            a-=c[j-1]*(i-last[i][j])
        srate.append(a)
    m=int(input())
    for i in range(m):
        D,Q=map(int,input().split())
        bt=T[D]
        pa=0
        for j in range(D,d+1):
            if T[j]!=Q:
                pa+=c[Q-1]*(j-last[j][Q])
                last[j][Q]=D
                pa-=c[Q-1]*(j-last[j][Q])
            if T[j]!=bt or j==D:
                pa+=c[bt-1]*(j-D)
                last[j][bt]=last[j-1][bt]
                pa-=c[bt-1]*(j-last[j][bt])
            if j==D:
                pa+=s[j-1][Q-1]-s[j-1][bt-1]
                T[j]=Q
            srate[j]+=pa
        print(srate[-1])

if __name__ == '__main__':
    main()

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())
dq = [list(map(int, input().split())) for i in range(M)]
for m in range(M):
    t[dq[m][0]-1] = dq[m][1]
    last = [0 for i in range(len(c))]
    value = sum([s[i][t[i]-1] for i in range(D)])
    for i in range(D):
        last[t[i]-1] = i+1
        value -= sum([c[j]*(i+1 - last[j]) for j in range(26)])
    print(value)

Submission status: Time Limit Exceeded

Solution code:
def get_score(dec_type, inc_day_type, type_day):
	score = 0
	lastday_type = [-1] * 26
	for day, type in enumerate(type_day):
		lastday_type[type] = day
		score += inc_day_type[day][type]
		for i in range(26):
			score -= dec_type[i] * (day - lastday_type[i])
	return score
def mod_type_day(type_day, day, type):
	res = type_day.copy()
	res[day-1] = type
	return res

def resolve():
	D = int(input())
	# c: 各コンテストタイプの満足度の下がりやすさ
	c = list(map(int, input().split()))
	# s: 各日程に各コンテストを開催した時の満足度増加量
	s = []
	for _ in range(D):
		s.append(list(map(int, input().split())))
	# t: 各日程のコンテストタイプ
	t = []
	for _ in range(D):
		t.append(int(input())-1)
		# print(get_score(c,s,t))
	# M: 変更クエリの回数
	M = int(input())
	for _ in range(M):
		# d: 変更する日、q: 変更するコンテストタイプ
		d, q = map(int, input().split())
		t = mod_type_day(t, d, q-1)
		v = get_score(c, s, t)
		print(v)
resolve()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def SUM(n):
    return (1+n)*n//2


D=int(input())
c=np.array(list(map(int,input().split())))
s=[list(map(int,input().split())) for _ in range(D)]
#t=[np.random.randint(1,27) 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 _  in range(M):
    #d=np.random.randint(1,D+1)
    #q=np.random.randint(1,27)
    d=DQ[_][0]
    q=DQ[_][1]
    contest=[[] for _ in range(26)]
    for i in range(D):
        if i==d-1:
            contest[q-1].append(d)
            continue
        contest[t[i]-1].append(i+1)
    v=0
    for i in range(26):
        pre=0
        minus=0
        for j in contest[i]:
            v+=s[j-1][i]
            minus+=SUM(j-pre-1)*c[i]
            pre=j
        minus+=SUM(D-pre)*c[i]
        v-=minus
    t[d-1]=q
    print(v)

Submission status: Time Limit Exceeded

Solution code:
#input
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())
dq = [list(map(int, input().split())) for _ in range(M)]

"""
# score calc @ each day
for day in range(D):
    v += s[day][t[day]-1] 
    last[t[day]-1] = day + 1
    for j in range(len(c)):
        v -= c[j]*(day + 1 - last[j])
    print(v)
"""
for m in range(M):
    #variables
    v = 0
    last = [0 for _ in range(len(c))]
    # replace day:dq[m][0] test to q:dq[m][1]
    t[dq[m][0]-1] = dq[m][1]
    # score calc @ last day
    for day in range(D):
        v += s[day][t[day]-1] 
        last[t[day]-1] = day + 1
        for j in range(len(c)):
            v -= c[j]*(day + 1 - last[j])
    print(v)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
f=lambda:list(map(int,input().split()))
D=int(input())
c=f()
S,t=[],[]
for i in range(D):
  S.append(f())
for i in range(D):
  t.append(int(input()))
m=int(input())
for i in range(m):
  d,q=f()
  t[d-1]=q
  v=0
  l=[0]*26
  for i in range(D):
    l[t[i]-1]=i+1
    v+=S[i][t[i]-1]
    for j in range(26):
      v-=c[j]*(i+1-l[j])
  print(v)

Submission status: Time Limit Exceeded

Solution code:
import sys
import time
import random
import math

t_start = time.perf_counter()

N = 26
D = int(input())

c_list = list(map(int, input().split()))

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

s_table = list(zip(*s_table))

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

def calc(data):
	satisfaction = 0
	cache = []
	for k, c in enumerate(c_list):
		last = -1
		penalties = 0
		s_list = s_table[k]
		s = 0
		for i, j in enumerate(data):
			if j == k:
				s += s_list[i]
				last = i
			else:
				penalties += i - last
		satisfaction += s - penalties * c
		cache.append((s, penalties))
	return satisfaction, cache

def calc_update(data, cache, changed1, changed2):
	satisfaction = 0
	for k, c in enumerate(c_list):
		if k == changed1 or k == changed2:
			last = -1
			penalties = 0
			s_list = s_table[k]
			s = 0
			for i, j in enumerate(data):
				if j == k:
					s += s_list[i]
					last = i
				else:
					penalties += i - last
			cache[k] = (s, penalties)
		else:
			s, penalties = cache[k]
		satisfaction += s - penalties * c
	return satisfaction

satisfaction, cache = calc(data)

M = int(input())
for _ in range(M):
	i, j = map(int, input().split())
	i -= 1
	j -= 1
	org = data[i]
	data[i] = j
	satisfaction = calc_update(data, cache, j, org)
	print(satisfaction)

Submission status: Time Limit Exceeded

Solution code:
#C
D = int(input())
c = [0] + list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]
t = [0] + [int(input()) for _ in range(D)]

M = int(input())
for _ in range(M):
    last = [0] * 27
    manzoku = [0] * 27
    d, q = list(map(int, input().split()))
    t[d] = q
    for i in range(1, D+1):
        manzoku[t[i]] += s[i-1][t[i]-1]
        last[t[i]] = i
        result = 0
        for j in range(1, 27):
            manzoku[j] -= c[j] * (i - last[j])
            result += manzoku[j]

    print(result)

Submission status: Time Limit Exceeded

Solution code:
d=int(input())
c=input()
cl=[int(n) for n in c.split()]
sl=list()
for i in range(d):
    s=input()
    sl=sl+[int(n) for n in s.split()]
tl=list()
for i in range(d):
    t=int(input())
    tl.append(t)
sat=0
for i in range(d):
    sat+=sl[i*26+tl[i]-1]

def decr(tl,cl):
    dec=0
    for i in range(26):
        last=[a for a, x in enumerate(tl) if x==i+1]
        if last==list():
            dec+=(d*(d+1)/2)*cl[i]
        else:
            for v in range(len(last)):
                if v!=len(last)-1:    
                    dec+=cl[i]*(last[v+1]-last[v]-1)*(last[v+1]-last[v])/2
                else:
                    dec+=cl[i]*(d-last[v]-1)*(d-last[v])/2
            dec+=cl[i]*(last[0])*(last[0]+1)/2
    return(int(dec))

score=sat
m=int(input())
for k in range(m):
    q=input()
    q0=q.split()
    da=int(q0[0])
    q=int(q0[1])
    cha=sl[(da-1)*26+q-1]-sl[(da-1)*26+tl[da-1]-1]
    tl[da-1]=q
    score+=cha
    print(score-decr(tl,cl))

Submission status: Time Limit Exceeded

Solution code:
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)]

def init():
    global 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

def diff(d, q):
    global score, T, C, S
    p = T[d - 1]
    score += S[d - 1][q - 1] - S[d - 1][p - 1]
    bef_p, aft_p = 0, D + 1
    bef_q, aft_q = 0, D + 1
    check_p, check_q = False, False
    for i, t in enumerate(T, 1):
        if t == p and i < d: bef_p = i
        elif not check_p and t == p and i > d: aft_p = i; check_p = True
        if t == q and i < d: bef_q = i
        elif not check_q and t == q and i > d: aft_q = i; check_q = True
        if check_p and check_q: break
    score += C[q - 1] * (d - bef_q) * (aft_q - d) - C[p - 1] * (d - bef_p) * (aft_p - d)
    T[d - 1] = q
    return score
    
score = init()
M = int(input())
for _ in range(M):
    d, q = map(int, input().split())
    print(diff(d, q))

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())
dq = [list(map(int, input().split())) for i in range(M)]
for m in range(M):
    t[dq[m][0]-1] = dq[m][1]
    last = [0 for i in range(len(c))]
    value = 0
    for i in range(D):
        value += s[i][t[i]-1]
        last[t[i]-1] = i+1
        for j in range(26):
            value -= c[j]*(i+1 - last[j])
    print(value)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
f=lambda:[*map(int,input().split())]
D=int(input())
c=f()
S,t=[],[]
for i in range(D): S+=[f()]
for i in range(D): t+=[int(input())]
m=int(input())
for i in range(m):
  d,q=f()
  t[d-1]=q
  v=0
  l=[0]*26
  for i in range(D):
    l[t[i]-1]=i+1
    v+=S[i][t[i]-1]
    for j in range(26):
      v-=c[j]*(i+1-l[j])
  print(v)

Submission status: Time Limit Exceeded

Solution code:
D = int(input())
C = input().split()
C = [int(c) for c in C]
S = []
for i in range(D):
  s = input().split()
  s = [int(j) for j in s]
  S.append(s)
T = []
for i in range(D):
  T.append(int(input()))
def value():
  M = 0
  last = [0] * 26
  for d in range(1,D+1):
    N = 0
    t = T[d-1]
    last[t-1] = d
    #print(last)
    for j in range(26):
      N = N + C[j]*(d-last[j])
    M = M + S[d-1][t-1] - N
  return(M)
P = int(input())
for i in range(P):
  Q = input().split()
  Q = [int(q) for q in Q]
  T[Q[0]-1] = Q[1]
  print(value())

Submission status: Time Limit Exceeded

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

a = 0
ans = []

t_array = [int(input())-1 for i in range(d)]

m = int(input())
for _ in range(m):
    day,q = map(int,input().split())
    a = 0
    last = [0]*n
    t_array[day-1] = q-1
    for i in range(d):
        t = t_array[i]
        a += s[i][t]
        last[t] = i+1
        for j in range(n):
            a -= c[j]*(i-last[j]+1)
    print(a)