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: Wrong Answer

Solution code:
D = int(input())
c = list(map(int, input().split()))
s = []
for i in range(D):
    s.append(list(map(int, input().split())))
t = []
for i in range(D):
    t.append(int(input()))
m = int(input())
dq = []
for i in range(m):
    dq.append(list(map(int, input().split())))
last0 = []
last = [0] * 26
score = 0
for i in range(D):
    contest = t[i] - 1
    score += s[i][contest]
    last = [last[i] + 1 for i in range(26)]
    last[contest] = 0
    last0.append(last)
    for j in range(26):
        score -= c[j] * last[j]
for i in range(m):
    d, q = dq[i][0] - 1, dq[i][1] - 1
    contest = t[d] - 1
    score += s[d][q]
    score -= s[d][contest]
    diff = 0
    num = last0[d][q] - last0[d][contest]
    for j in range(d, D):
        if last0[j][q] == 0:
            break
        else:
            diff += c[q] * num
            last0[j][q] -= num
    for j in range(d, D):
        if last0[j][contest] == 0:
            if j == d:
                diff -= c[contest] * num
                last0[j][contest] += num
                pass
            else:
                break
        else:
            diff -= c[contest] * num
            last0[j][contest] += num
    score += diff
    t[d] = q + 1
    print(score)

Submission status: Wrong Answer

Solution code:
import copy
D=int(input())
ListC = list(map(int, input().split()))
dictC={}
dictD = {}
for i in range (1,27):
  dictC[i] = int(0)
  dictD[i] = int(0)
ListS = []
for i in range (D):
   ListS.append(list(map(int, input().split())))
ListT = []
for i in range (D):
   ListT.append(int(input()))
M = int(input())
ListM = []
for i in range (M):
   ListM.append(list(map(int, input().split())))

def calc_figure(list_s, list_t, list_c, dict_c):
  res = 0
  for j in range(D):
    res += list_s[j][list_t[j]-1]
    for i in range(1,27):
      if list_t[j] == i:
        pass
      else:
        res += -1*(j+1-dict_c[i])*list_c[i-1]
    dict_c[list_t[j]] = j+1
  return res

ListT2 = copy.deepcopy(ListT)
for i in range(M):
  dictC = copy.deepcopy(dictC)
  ListT2[ListM[i][0]-1] = ListM[i][1]
  print(calc_figure(ListS, ListT2, ListC, dictC))

Submission status: Wrong Answer

Solution code:
D = int(input())
C = list(map(int, input().split()))
S = list()
for i in range(D):
    tmp_ = list(map(int, input().split()))
    S.append(tmp_)
T = list()
for i in range(D):
    T.append(int(input()))
ret = 0
lastd = [0] * 26
for i in range(D):
    ret += S[i][T[i]-1]
    lastd[T[i] - 1] = i + 1
    for j in range(26):
        ret -= C[j] * (i + 1 - lastd[j])
M = int(input())

for m in range(M):
    d,q = map(int,input().split())
    d_ , q_ = d - 1, q - 1
    olds = T[d_]
    T[d_] = q
    td_ = 0
    for i in range(D - d):
        td_ += i + 1
    ret -= S[d_][olds - 1]
    ret += S[d_][T[d_] - 1]
    ret += (C[olds - 1] - C[T[d_] - 1]) * td_
    print(ret)

Submission status: Wrong Answer

Solution code:
D = int(input())
c = list(map(int, input().split()))
s = []
for i in range(D):
    s.append(list(map(int, input().split())))
t = []
for i in range(D):
    t.append(int(input()))
m = int(input())
dq = []
for i in range(m):
    dq.append(list(map(int, input().split())))
Last = []
last = [0] * 26
score = 0
for i in range(D):
    contest = t[i] - 1
    score += s[i][contest]
    last = [last[i] + 1 for i in range(26)]
    last[contest] = 0
    Last.append(last)
    for j in range(26):
        score -= c[j] * last[j]
for i in range(m):
    d, q = dq[i][0] - 1, dq[i][1] - 1
    contest = t[d] - 1
    score += s[d][q]
    score -= s[d][contest]
    diff = 0
    num = Last[d][q] - Last[d][contest]
    for j in range(d, D):
        if Last[j][q] == 0:
            break
        else:
            diff += c[q] * num
            Last[j][q] -= num
    for j in range(d, D):
        if Last[j][contest] == 0:
            if j == d:
                diff -= c[contest] * num
                Last[j][contest] += num
                pass
            else:
                break
        else:
            diff -= c[contest] * num
            Last[j][contest] += num
    score += diff
    t[d] = q + 1
    print(score)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right

class Set(): #BinaryIndexedTree
    def __init__(self, valuelist, INF=10**18+1):
        self.n = len(valuelist) + 1
        self.value = sorted(valuelist) + [INF]
        self.comp = {v : k for k, v in enumerate(self.value, 1)}
        self.bit = BinaryIndexedTree(self.n)

    def insert(self, x, k=1):
        self.bit.add(self.comp[x], k)

    def delete(self, x, k=1):
        self.bit.add(self.comp[x], -k)

    def count(self, x):
        return self.bit.get(self.comp[x])

    def sum(self, x):
        return self.bit.sum(self.comp[x])

    def get(self, k): #k個以下のときはINFを返す
        return self.value[self.bit.bisect_left(k) - 1]

class BinaryIndexedTree(): #1-indexed
    def __init__(self, n):
        self.n = n
        self.tree = [0 for _ in range(n + 1)]

    def sum(self, idx):
        res = 0
        while idx:
            res += self.tree[idx]
            idx -= idx & -idx
        return res

    def get(self, idx):
        return self.sum(idx) - self.sum(idx - 1)

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

    def bisect_left(self, x):
        if x <= 0: return 0
        res, tmp = 0, 2**((self.n).bit_length() - 1)
        while tmp:
            if res + tmp <= self.n and self.tree[res + tmp] < x:
                x -= self.tree[res + tmp]
                res += tmp
            tmp >>= 1
        if res >= self.n:
            return self.n
        return res + 1

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

res = 0
last = [0 for _ in range(26)]
T = [0 for _ in range(D)]
used = [Set(list(range(D))) for i in range(26)]

for d in range(D):
    maxtmp = -10**18
    contest = None
    for i in range(26):
        tmp = res
        lasttmp = last[:]
        tmp += S[d][i]
        lasttmp[i] = d + 1
        for j in range(26):
            tmp -= C[j] * ((d + 1) - lasttmp[j])
        if maxtmp <= tmp:
            maxtmp = tmp
            contest = i
    res += S[d][contest]
    last[contest] = d + 1
    used[contest].insert(d)
    for j in range(26):
        res -= C[j] * ((d + 1) - last[j])
    T[d] = contest + 1

from random import randint

for i in range(200000):
    tmp = res
    d = randint(1, D)
    q = randint(1, 26)
    if T[d - 1] == q:
        continue
    tmp -= S[d - 1][T[d - 1] - 1]
    tmp += S[d - 1][q - 1]
    k = used[T[d - 1] - 1].sum(d - 1)
    l = used[T[d - 1] - 1].get(k - 1) if k != 1 else -1
    r = used[T[d - 1] - 1].get(k + 1)
    if r == 1000000000000000001:
        r = D
    tmp -= (d - 1 - l) * C[T[d - 1] - 1] * (r - d + 1)
    used[T[d - 1] - 1].delete(d - 1)
    used[q - 1].insert(d - 1)
    k = used[q - 1].sum(d - 1)
    l = used[q - 1].get(k - 1) if k != 1 else -1
    r = used[q - 1].get(k + 1)
    if r == 1000000000000000001:
        r = D
    tmp += (d - 1 - l) * C[q - 1] * (r - d + 1)
    if tmp > res:
        res = tmp
        T[d - 1] = q
    else:
        used[T[d - 1] - 1].insert(d - 1)
        used[q - 1].delete(d - 1)

for r in T:
    print(r)

Submission status: Wrong Answer

Solution code:
d = int(input())
c = list(map(int,input().split()))
s = []
day = [0 for i in range(26)]
for i in range(d):
    s.append(list(map(int,input().split())))
ans = 0
num = 1
for i in s:
    max_index = -1
    large = 0
    for j in range(26):
        if large < i[j]:
            large = i[j]
            if not max_index == -1:
                ans -= c[max_index]*(num-day[max_index])
            max_index = j
        else:
            ans -= c[j]*(num-day[j])
    day[max_index] = num
    ans += large
    print(max_index+1)
    num += 1

Submission status: Wrong Answer

Solution code:
D = int(input())

last_list = [0 for i in range(27)]
c_list = list(map(int, input().split()))
c_list = [""] + c_list
# print(c_list)
S_list = [[]for i in range(D)]
P_list = [0 for i in range(27)]
for i in range(D):
    S_list[i] = list(map(int, input().split()))
    S_list[i] = [""] + S_list[i]
S_list = [""] + S_list
# print(S_list)


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

for i in range(1,D+1):
    S = 0
    t = t_list[i]
    for j in range(1,27):
        if j == t:
            P_list[j] += S_list[i][j]
            last_list[j] = i
            # print(S_list[i][j])
        else:
            P_list[j] -= c_list[j] * (i-last_list[j])
    for i in range(1,27):
        S += P_list[i]
    # print(S)

M = int(input())
print(M)
for m in range(M):
    last_list = [0 for i in range(27)]
    P_list = [0 for i in range(27)]
    C_A, C_B = map(int,input().split())
    t_new = t_list
    t_new[C_A] = C_B
    for i in range(1,D+1):
        S_new = 0
        t = t_new[i]
        for j in range(1,27):
            if j == t:
                P_list[j] += S_list[i][j]
                last_list[j] = i
                # print(S_list[i][j])
            else:
                P_list[j] -= c_list[j] * (i-last_list[j])
        for i in range(1,27):
            S_new += P_list[i]
    print(S_new)

Submission status: Wrong Answer

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

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

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

m = int(input())
M = []
for i in range(m):
    M.append(list(map(int, input().split())))
print(M)

for m_ in range(m):
    T[M[m_][0] - 1] = M[m_][1]

    distance_list = [0] * 26
    sum_ = 0
    for i in range(D):
        sum_ += S[i][T[i]-1]
        down = 0
        for j in range(26):
            if T[i] - 1 == j:
                distance_list[j] = 0
                continue
            distance_list[j] += 1
            down += distance_list[j] * C[j]
        sum_ -= down

    print(sum_)

Submission status: Wrong Answer

Solution code:
D = int(input())#D=365
c = list(map(int, input().split()))#0<=c<=100
s = [list(map(int, input().split())) for _ in range(D)]#0<=s<=20000

t = [int(input()) for _ in range(D)]

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


def t2v(t):
    v = []
    last = [0] * 26
    value = 0
    for d in range(D):
        type = t[d]
        value += s[d][type - 1]
        last[type - 1] = d + 1
        for i in range(26):
            value -= c[i] * (d + 1 - last[i])
        v.append(value)
    return v


v_lastday = []
v = t2v(t)
v_lastday.append(v[-1])
for d, q in dq:
    t[d - 1] = q
    v = t2v(t)
    v_lastday.append(v[-1])

for x in v_lastday:
    print(x)


"""
for i in range(D):
    print(v[i])
"""

Submission status: Wrong Answer

Solution code:
D=int(input())
c=input().split()
c=[int(a) for a in c]
S=[]
for _ in range(D):
    s=input().split()
    s=[int(a) for a in s]
    S.append(s)
T=[]
for _ in range(D):
    t=int(input())-1
    T.append(t)
M=int(input())
numbers=[]
for _ in range(M):
    d,q=map(int,input().split())
    numbers.append([d,q])
whole=sum(c)
for m in range(M):
    ans = 0
    held = [0 for _ in range(26)]
    e=numbers[m][0]-1
    q=numbers[m][1]-1
    a=T[e]
    T[e]=q
    number=[]
    for d in range(D):
        s = S[d]
        place = T[d]
        held[place] = d + 1
        ans += s[place]
        ans-=whole*(d+1)
        ans+=c[place]*(d+5)
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

D = int(input()) 
c = list(map(int,input().split())) 
s = [list(map(int,input().split())) for i in range(D)]
t = [int(input().strip()) for _ in range(D)]
M = int(input())
d = [list(map(int,input().split())) for i in range(M)]

last = np.zeros(26)
ans = 0

for i in range(M):
    t[d[i][0]-1] = d[i][1]

for i in range(D):
    last[t[i]-1] = i+1
    dec = c * (i+1-last)
    ans = ans + s[i][t[i]-1] - sum(dec)
    print(int(ans))

Submission status: Wrong Answer

Solution code:
t = [16,8,17,7,8,21,13,5,1,25,12,14,12,6,4,24,6,9,18,3,22,19,3,24,7,7,2,23,11,6,14,17,4,21,24,23,6,14,6,15,26,22,19,18,20,11,10,16,18,5,16,7,7,24,18,16,14,18,22,25,19,20,13,23,3,25,19,25,25,3,17,16,5,22,9,11,5,6,6,13,26,14,9,13,11,11,1,19,22,23,19,18,5,14,26,11,25,3,24,6,24,20,12,19,7,25,16,19,6,1,4,16,15,15,5,6,24,26,2,3,15,5,10,21,22,16,22,1,2,15,25,7,16,15,5,25,12,6,16,2,14,16,12,10,22,18,22,9,16,25,1,13,25,5,8,23,17,14,17,18,17,16,13,24,21,10,10,12,15,15,22,1,15,23,21,24,23,8,13,19,14,3,2,24,3,15,11,19,6,26,11,22,1,20,24,4,11,14,12,25,10,25,8,6,9,24,10,19,23,14,11,11,21,6,14,21,24,24,26,18,26,2,13,14,13,7,17,15,15,11,5,20,12,25,8,14,11,15,1,21,7,26,17,18,3,4,4,9,21,20,5,9,15,10,23,1,25,25,26,16,22,19,3,13,16,19,1,10,12,6,22,7,21,2,6,1,1,15,22,12,26,25,12,14,20,18,11,4,3,16,23,19,18,20,5,19,10,23,8,4,9,26,3,15,2,26,3,14,6,23,12,17,9,10,20,22,4,23,22,24,8,18,3,2,12,4,22,7,25,12,23,7,2,21,19,2,13,2,15,18,24,20,12,8,3,19,13,16,24,15,8,7,13,7,14,11,5,21,6,4,24,15,20,15,7]
for i in range(365):
    print(t[i])

Submission status: Wrong Answer

Solution code:
d=int(input())

last=[0]*26
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())
change=[list(map(int,input().split())) for _ in range(m)]
ans=[]

for [dd,q] in change:
    t[dd-1]=q
    #print(t)
    score=0
    for i in range(d):
        #print(i)
        score+=s[i][t[i]-1]
        last[t[i]-1]=i+1
        for j in range(26):
            score-=(c[j]*((i+1)-last[j]))
        if dd-1==i:
            break
            
    ans.append(score)


for item in ans:
    print(item)

Submission status: Wrong Answer

Solution code:
def c_incremental_scoring():
    """B 問題の入力と、追加で「d 日目のコンテストのタイプを q に変更するクエリ」が
    与えられるので、それに基づいて日毎の満足度を計算せよ。
    (A 問題では (大雑把に言って) クエリをうまく与えて満足度を可能な限り高くしていく)"""

    D = int(input())
    Contests = [int(i) for i in input().split()]
    Satisfaction = [[int(i) for i in input().split()] for j in range(D)]
    Contest_type = [int(input()) for _ in range(D)]
    M = int(input())
    Queries = [[int(i) for i in input().split()] for j in range(M)]

    ans = []
    s = 0
    last = {i: 0 for i in range(1, 27)}

    for d, q in Queries:
        Contest_type[d - 1] = q

    for d, t in enumerate(Contest_type, 1):
        s += Satisfaction[d - 1][t - 1]
        last[t] = d

        satisfaction_decreasing = 0
        for i in range(1, 27):
            satisfaction_decreasing += Contests[i - 1] * (d - last[i])
        s -= satisfaction_decreasing
        ans.append(s)
    return '\n'.join(map(str, ans))

print(c_incremental_scoring())

Submission status: Wrong Answer

Solution code:
import numpy as np

D = int(input()) 
c = list(map(int,input().split())) 
s = [list(map(int,input().split())) for i in range(D)]
t = [int(input().strip()) for _ in range(D)]
M = int(input())
d = [list(map(int,input().split())) for i in range(M)]

last = np.zeros(26)
ans = 0

for i in range(M):
    t[d[i][0]-1] = d[i][1]

for i in range(D):
    last[t[i]-1] = i+1
    dec = c * (i+1-last)
    ans = ans + s[i][t[i]-1] - sum(dec)
    print(ans)

Submission status: Wrong Answer

Solution code:

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

    T = []
    for j in range(D):
        T.append(int(input()))
    
    ans = cal_score(S,C,D,T)
    print(ans)

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

def cal_score(S,C,D,T):
    ans = 0
    ans_l = []
    last = [0] * 26
    for j,t in enumerate(T):
        j += 1
        last[t-1] = j
        SUM = 0
        for i in range(26):
            SUM += C[i] * (j-last[i])
        ans -= SUM
        ans += S[j-1][t-1]
        ans_l.append(ans)
    return ans_l[-1]


main()

Submission status: Wrong Answer

Solution code:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    d = int(ipt())
    c = [int(i) for i in ipt().split()]
    s = [[int(i) for i in ipt().split()] for _ in range(d)]
    ld = [-1]*26
    edval = [0]*26

    for i in range(d):
        edval = s[i]*1
        for j in range(26):
            dl = i-ld[j]
            edval[j] += c[j]*dl*(dl+1)//2*0.75
        v = edval.index(max(edval))
        ld[v] = i
        print(v+1)

    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def c_incremental_scoring():
    """B 問題の入力と、追加で「d 日目のコンテストのタイプを q に変更するクエリ」が
    与えられるので、それに基づいて日毎の満足度を計算せよ。
    (A 問題では (大雑把に言って) クエリをうまく与えて満足度を可能な限り高くしていく)"""

    D = int(input())
    Contests = [int(i) for i in input().split()]
    Satisfaction = [[int(i) for i in input().split()] for j in range(D)]
    Contest_type = [int(input()) for _ in range(D)]
    M = int(input())
    Queries = [[int(i) for i in input().split()] for j in range(M)]

    ans = []
    s = 0
    last = {i: 0 for i in range(1, 27)}

    for d, q in Queries:
        Contest_type[d - 1] = q

    for d, t in enumerate(Contest_type, 1):
        print(d, t)
        s += Satisfaction[d - 1][t - 1]
        last[t] = d

        satisfaction_decreasing = 0
        for i in range(1, 27):
            satisfaction_decreasing += Contests[i - 1] * (d - last[i])
        s -= satisfaction_decreasing
        ans.append(s)
    return '\n'.join(map(str, ans))

print(c_incremental_scoring())

Submission status: Wrong Answer

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]
print(P-sum(Y))
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]
    X[T[d]]+=C[T[d]]
    X[q]+=C[q]
    X[Z]=0
    Y[T[d]]+=X[T[d]]
    Y[q]+=X[q]
  T[d]=q
  print(P-sum(Y))

Submission status: Wrong Answer

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

s=[None]*D
for i in range(D):
    s[i] = list(map(int,input().split()))
t=[0]*D
for i in range(D):
    t[i]=int(input())
    
m=int(input())
d=[None]*m
q=[None]*m
for i in range(m):
    d[i],q[i] = map(int,input().split())

def calc_score(t):
    last=[0]*26
    satis=0
    pena=0
    for j in range(D):
        ss=s[j]
        tt=t[j]
        satis += ss[tt-1]
        
        last[tt-1]=j+1
        
        pena += sum(c[i]*(j+1-last[i]) for i in range(26))
    return max(0,10**6+satis-pena)
  
for i in range(m):
    t[d[i]-1] = q[i]
    print(calc_score(t)-10**6)

Submission status: Wrong Answer

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

score = 0
last = [0]*26
for d in range(D):
    score += s[d][t[d]-1]
    last[t[d]-1] = d+1
    for i in range(26):
        score -= c[i]*(d+1-last[i])
    print(score)

Submission status: Wrong Answer

Solution code:
0

Submission status: Time Limit Exceeded

Solution code:

def calcScore():
    for d in range(1, D + 1):
        id = t[d]  # この日に開催するコンテスト
        sinceLast[d][id] = 0
        score[d][id] = s[d][id]


        for i in range(1, 26 + 1):
            if i == id:  continue
            sinceLast[d][i] = sinceLast[d - 1][i] + 1
            score[d][i] = -c[i] * sinceLast[d][i]

    ans = 0
    for d in range(1, D + 1):
        ans += sum(score[d])

    return ans

def calcScoreDiff(score, d, before, after):
    ans = 0
    for dd in range(d, D + 1):
        if dd > d and t[dd] == before:  break
        sinceLast[dd][before] = sinceLast[dd - 1][before] + 1
        ans -= score[dd][before]
        score[dd][before] = -c[before] * sinceLast[dd][before]
        ans += score[dd][before]

    ans -= score[d][after]
    sinceLast[d][after] = 0
    score[d][after] = s[d][after]
    ans += score[d][after]

    for dd in range(d + 1, D + 1):
        if t[dd] == after:  break
        sinceLast[dd][after] = sinceLast[dd - 1][after] + 1
        ans -= score[dd][after]
        score[dd][after] = -c[after] * sinceLast[dd][after]
        ans += score[dd][after]

    return ans

D = int(input())
c = [0] + [int(x) for x in input().split()]
s = [[0 for _ in range(26 + 1)]] + \
    [[0] + [int(x) for x in input().split()] for _ in range(D)]
t = [0] + [int(input()) for _ in range(D)]

score = [[0] * (26 + 1) for _ in range(D + 1)]
sinceLast = [[0] * (26 + 1) for _ in range(D + 1)]
tot = calcScore()

M = int(input())
for i in range(M):
    d, after = [int(x) for x in input().split()]
    before = t[d]
    t[d] = after
    diff = calcScoreDiff(score, d, before, after)
    tot += diff
    print(tot)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

def penalty(day, last, c):
    val = 0
    for i in range(26):
        val += c[i] * (day - last[i])
    return val

def calcScore(t, s, c):
    val = 0
    for i in range(26):
        last = 0
        for j in range(len(t)):
            if t[j] == i:
                val += s[j][i]
                val -= c[i]*(j-last)*(j-last-1)//2
                last = j
        val -= c[i]*(len(t)-last)*(len(t)-last-1)//2
    return val

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

score = calcScore(t, s, c)

ans = [None]*M
for i in range(M):
    d, q = map(int, input().split())
    t[d] = q-1
    ans[i] = calcScore(t, s, c)

for v in ans:
    print(v)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

def penalty(day, last, c):
    val = 0
    for i in range(26):
        val += c[i] * (day - last[i])
    return val

def update(day, t, q, score, c, last):
    t[day] = q
    for i in range(day, len(score)):
        for j in range(26):
            last[i][j] = last[i-1][j]
        score[i] = score[i-1] + s[i][t[i]]
        last[i][t[i]] = i
        score[i] -= penalty(i, last[i], c)

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

last = [[0]*26 for _ in range(D+1)]
score = [0]*(D+1)

for i in range(1, D+1):
    for j in range(26):
        last[i][j] = last[i-1][j]
    score[i] = score[i-1] + s[i][t[i]]
    last[i][t[i]] = i
    score[i] -= penalty(i, last[i], c)

ans = [None]*M
for i in range(M):
    d, q = map(int, input().split())
    update(d, t, q-1, score, c, last)
    ans[i] = score[-1]

for v in ans:
    print(v)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

def penalty(day, last, c):
    val = 0
    for i in range(26):
        val += c[i] * (day - last[i])
    return val

def update(day, t, q, score, c, last):
    t[day] = q
    for i in range(day, len(score)):
        last[i] = deepcopy(last[i-1])
        score[i] = score[i-1] + s[i][t[i]]
        last[i][t[i]] = i
        score[i] -= penalty(i, last[i], c)

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

last = [[0]*26 for _ in range(D+1)]
score = [0]*(D+1)

for i in range(1, D+1):
    last[i] = deepcopy(last[i-1])
    score[i] = score[i-1] + s[i][t[i]]
    last[i][t[i]] = i
    score[i] -= penalty(i, last[i], c)

ans = [None]*M
for i in range(M):
    d, q = map(int, input().split())
    update(d, t, q-1, score, c, last)
    ans[i] = score[-1]

for v in ans:
    print(v)

Submission status: Time Limit Exceeded

Solution code:
(D,),C,*I=[[*map(int,t.split())]for t in open(0)]
s=sum(C)
for d,q in I[D-~D:]:I[d-1+D][0]=q;i,R,P,*L=[0]*29;exec('t=I[i+D][0]-1;P+=s+(~i+L[t])*C[t];R+=I[i][t]-P;i+=1;L[t]=i;'*D);print(R)