Problem:


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

Beginner's Guide

Let's first write a program to calculate the score from a pair of input and output. You can know the total score by submitting your solution, or an official program to calculate a score is often provided for local evaluation as in this contest. Nevertheless, writing a score calculator by yourself is still useful to check your understanding of the problem specification. Moreover, the source code of the score calculator can often be reused for solving the problem or debugging your solution. So it is worthwhile to write a score calculator unless it is very complicated.

Problem Statement

You will be given a contest schedule for D days.
For each d=1,2,\ldots,D, calculate the satisfaction at the end of day d.

Input

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

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

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

For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints.

Output

Let v_d be the satisfaction at the end of day d.
Print D integers v_d to Standard Output in the following format:

v_1
v_2
\vdots
v_D

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

Sample Output 1

18398
35037
51140
65837
79325

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

We can build a solution (schedule) for this problem in the order of day 1, day 2, and so on. And for every partial solution we have built, we can calculate the goodness (satisfaction) by using the above score calculator. So we can construct the following algorithm: for each d=1,2,\ldots,D, we select the contest type that maximizes the satisfaction at the end of day d. You may have already encountered this kind of "greedy algorithms" in algorithm contests such as ABC. Greedy algorithms can guarantee the optimality for several problems, but unfortunately, it doesn't ensure optimality for this problem. However, even if it does not ensure optimality, we can still obtain a reasonable solution in many cases. Let's go back to Problem A and implement the greedy algorithm by utilizing the score calculator you just implemented!

Greedy methods can be applied to a variety of problems, are easy to implement, and often run relatively fast compared to other methods. Greedy is often the most powerful method when we need to process huge inputs.
We can further improve the score by changing the greedy selection criteria (evaluation function), keeping multiple candidates instead of focusing on one best partial solution (beam search), or using the output of greedy algorithms as an initial solution of other methods.
For more information, 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


Sample output:
18398
35037
51140
65837
79325

Submission status: Accepted

Solution code:
import numpy as np


D = int(input())
c = np.array(list( map(int,input().split())))
S = np.zeros((D,26))
for i in range(D):
    S[i] = np.array(list( map(int,input().split())))

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

"""
D = 365
c = np.random.randint(0,100,size=(26))
S = np.random.randint(0,20000,size=(D,26))
"""

def calc_score(T,D,c,S):
    last = np.ones(26) * (-1)
    scores = []
    score = 0
    for d in range(D):
        i = int(T[d])-1
        score += S[d,i]
        last[i] = d
        score -= np.sum(c*(d-last))
        scores.append(score)
    score = max([10**6+score,0])
    return score,scores

_,ans = calc_score(T,D,c,S)
for a in ans:
    print(int(a))

Submission status: Accepted

Solution code:
D=int(input())
*c,=map(int,input().split())
# for Greedy
ci=[(c[i-1],i) for i in range(1,27)]
ci=[(0,0)]+ci
# ci=[(0,0)]+sorted(ci,reverse=True)

last=[[0]*27]*(D+1)
S=[[0]*27]*(D+1)
DP=[[0]*27]*(D+1)
T=[0]*(D+1)

for d in range(1,D+1):
    *S[d],=map(int,input().split())
    S[d]=[0]+S[d]
    
for d in range(1,D+1):
    T[d] = int(input())

def do_id(i,d):
    dn=0
    last[d][i]=d    
    for k in range(1,27):
      if k!=i:
        dn += ci[k][0]*(d-last[d][k])
    return S[d][i]-dn

Satis=0  
for d in range(1,D+1):
    Satis+=do_id(T[d],d)
    print(Satis)
  
def cal():  
  for d in range(1,D+1):
    mxid=0
    mxi,mxd=0,0
    for i in range(1,27):
      tc,ti=ci[i]
      doid=do_id(ti,d)
      if doid>mxid:
           mxid=doid
           mxi,mxd=ti,d
    DP[mxd][mxi]=mxid             
    last[mxd][mxi]=d
  print(DP)

Submission status: Accepted

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)]
last = [0 for i in range(26)]
#print(c)
res = 0
count = 0
#while count != len(t):
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])
  print(res)

Submission status: Accepted

Solution code:
(d,), cc, *sstt = [list(map(int, s.split())) for s in open(0)]

ss = sstt[:d]

tt = list(map(lambda x: x-1, sum(sstt[d:], [])))

comp = [0]*len(cc)

ans = 0

for i in range(d):
    comp = [sum(x) for x in zip(comp, cc)]
    t = tt[i]
    ans += ss[i][t]
    comp[t] = 0
    ans -= sum(comp)
    print(ans)

Submission status: Accepted

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

ans = [0]*d
last = [0]*26

def score(day, last, today, C, S):
    res = 0
    res += S[day][today]
    last[today] = day+1
    for i in range(26):
        res -= C[i]*(day+1 - last[i])
    return res        

for i in range(d):
    t = int(input())
    if i > 0:ans[i] += ans[i-1]
    ans[i] += score(i,last,t-1,C,S)
print("\n".join(str(i) for i in ans))

Submission status: Accepted

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

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

def calc(data):
	last = [-1] * N
	satisfaction = 0
	for i in range(D):
		j = data[i]
		satisfaction += s_table[i][j]
		last[j] = i
		for k in range(N):
			satisfaction -= c_list[k] * (i - last[k])
		print(satisfaction)
	return satisfaction

calc(data)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import numpy as np
import copy

D = int(input())
C = list(map(int,input().split()))
contest = [list(map(int,input().split())) for i in range(D)]
inputs = [int(input()) for i in range(D)]
# print(contest)
# print(inputs)

lists = np.array([0 for i in range(26)])
Y = 0
for i,j in zip(contest,inputs):
    lists += 1
    lists[j-1] = 0
    X = C * lists.T
    # print(X)
    # print(sum(X))
    # x = sum(C[:j-1])+sum(C[j:])
    # y = i[j-1]-x+solve
    Y = i[j-1]-sum(X)+Y
    print(Y)
    Y = copy.deepcopy(Y)

Submission status: Accepted

Solution code:
d=int(input())
a=[]
for x in range(d+1):
  b=list(map(int,input().split()))
  a.append(b)
nittei=[]
for x in range(d):
  nittei.append(int(input()))
  

sum1=sum(a[0][0:26])
ans=[0]*d
humann=a[0]*d
humann=humann+[0]*26



for n in range(d):
  date=nittei[n]
  ans[n]=(a[n+1][date-1])
  try:
    for z in range(26):
      humann[n*26+z]=int(humann[(n-1)*26+z])+int(humann[n*26+z])
  except:
    True
  humann[n*26+date-1]=0
  sum2=sum(humann[n*26+0:n*26+26])
  ans[n]=int(ans[n]) - int(sum2)
  
  
  try:
    ans[n]=ans[n]+ans[n-1]
  except:
    True
    
for y in range(d):
  print(ans[y])

Submission status: Accepted

Solution code:
D = int(input())
c = list(map(int, input().split()))
s = []
c_sum = sum(c)
for i in range(D):
    tmp = list(map(int, input().split()))
    s.append(tmp)
t = []
for i in range(D):
    tmp = int(input())
    t.append(tmp-1)
cnt = [-1 for j in range(26)]
now = 0
for i in range(D):
    cnt[t[i]] = i
    now += s[i][t[i]]
    tmp = 0
    for j in range(26):
        if j != t[i]:
            if cnt[t[i]] == -1:
                tmp += c[j] * (i + 1)
            else:
                tmp += c[j] * (i - cnt[j])
    now -= tmp
    print(now)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    D = int(input())
    c = [0] + list(map(int, input().split()))
    s = [[0]*27]
    t = [0]
    satisfy = 0
    last = [0] * 27
    for _ in range(D):
        s.append([0] + list(map(int, input().split())))
    for _ in range(D):
        t += [int(input())]
    for i in range(1, D+1):
        decrease = 0
        last[t[i]] = i
        for j in range(1, 27):
            decrease += (c[j] * (i - last[j]))
        satisfy += (s[i][t[i]] - decrease)
        print(satisfy)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import copy

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

V = []
for _ in range(D):
    v = int(input())
    V.append(v)

results = []

# 次の日に減る分も考慮して選ぶ
last = [0 for i in range(26)]
next_costs = [0 for i in range(26)]

result = 0
for d in range(D):
    for i in range(26):
        next_costs[i] = C[i] * (d+1-last[i])

    index = V[d] - 1
    next_costs_sum = sum(next_costs)
    result += S[d][index] - (next_costs_sum - C[index] * (d+1-last[index]))
    last[index] = d + 1
    results.append(result)

for result in results:
    print(result)

Submission status: Accepted

Solution code:
import time
import random


class Problem:

    def __init__(self, D, s, c):
        self.last_days = [0] * 26
        self.D = D
        self.s = s
        self.c = c
        self.answer = [0] * self.D
        self.best_answer = [0] * self.D
        self.best_score = 0
        self.days = [0] * self.D
        self.best_answer_scores = [0] * self.D

    def scoring(self, day, ind):
        day_score = self.s[day][ind]
        minus_score = 0
        for i in range(26):
            if i != ind:
                minus_score += self.c[i] * (day + 1 - self.last_days[i])
        day_score -= minus_score
        return day_score

    def get_all_scores(self, day):
        """全ての選択肢のscoreを返す"""
        minus_score = 0
        all_scores = [0] * 26
        for i in range(26):
            if i == 0:
                for j in range(1, 26):
                    minus_score += self.c[j] * (day + 1 - self.last_days[j])
                all_scores[0] = self.s[day][0] - minus_score

            else:
                minus_score += self.c[i - 1] * (day + 1 - self.last_days[i - 1])
                minus_score -= self.c[i] * (day + 1 - self.last_days[i])
                all_scores[i] = self.s[day][i] - minus_score
        return all_scores

    def _strategy_greedy(self, day):
        score = self.scoring(day, self.days[day])
        return score, self.days[day]

    def solve(self):
        self.init()

        sum_score = 0
        scores = []
        for i in range(self.D):
            score, ind = self._strategy_greedy(i)
            self.last_days[ind] = i + 1
            self.answer[i] = ind
            sum_score += score
            scores.append(sum_score)
        return sum_score, self.answer, scores

    def _strategy_perturbation(self):
        # ランダムに１日を取り出し、一番その日の期待コストが大きくなるよう変化させる
        # 貪欲法に摂動を加えてみる
        day = random.randint(0, 3)

        # scores = [self.scoring()]

        random.randint(0, 3)

    def solve_greed(self):
        self.init()

    def init(self):
        self.last_days = [0] * 26
        self.answer = [0] * self.D


def main():
    D = int(input())
    c = list(map(int, input().split()))
    s = []
    for _ in range(D):
        s.append(list(map(int, input().split())))
    days = []
    for _ in range(D):
        days.append(int(input()) - 1)

    max_score = 0

    problem = Problem(D, s, c)
    problem.days = days
    score, answer,scores = problem.solve()

    for i in range(D):
        # problem.best_answer[i] += 1
        # print(problem.best_answer[i])
        print(scores[i])
    # print(ite)
    # print(max_answer)
    # print(max_score)
    # print(problem.get_all_scores(1))


main()

Submission status: Accepted

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

d=[0]*(26)
v=[0]*(D+1)


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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from typing import NamedTuple, List

class Game(NamedTuple):
    c: List[int]
    s: List[List[int]]

def solve(D: int, c: "List[int]", s: "List[List[int]]", t: "List[int]"):
    from functools import reduce        
    ans = [0]
    lasts = [0] * 26
    sum_c = sum(c)
    daily_loss = sum_c
    bits = [BIT(D*2) for _ in range(26)]    
    for day, tt in enumerate(t, 1):
        a = s[day-1][tt-1]
        lasts[tt-1] = day
        for i in range(26):
            bits[i].add(day, lasts[i]*c[i])
        a -= daily_loss
        a += sum(b.sum(day)-b.sum(day-1) for b in bits)
        ans.append(ans[-1]+a)
        daily_loss += sum_c
    return ans[1:]

# https://kopricky.github.io/code/For_Python/bit.html
class BIT:
    def __init__(self, node_size):
        self._node = node_size+1
        self.bit = [0]*self._node
 
    def add(self, index, add_val):
        index += 1
        while index < self._node:
            self.bit[index] += add_val
            index += index & -index
 
    def sum(self, index):
        index += 1
        res = 0
        while index > 0:
            res += self.bit[index]
            index -= index & -index
        return res

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    D = int(next(tokens))  # type: int
    c = [int(next(tokens)) for _ in range(26)]  # type: "List[int]"
    s = [[int(next(tokens)) for _ in range(26)] for _ in range(D)]  # type: "List[List[int]]"
    t = [int(next(tokens)) for _ in range(D)]  # type: "List[int]"
    print(*solve(D, c, s, t), sep="\n")

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Accepted

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

socre = 0
his = [0] * len(C)

for i in range(D):
    t = int(input())
    t -= 1
    his[t] = i + 1
    sat = 0
    for j in range(len(C)):
        sat += C[j] * (i - his[j] + 1)
    socre += S[i][t] - sat

    print(socre)

Submission status: Accepted

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()))
last_di=[0]*26
m=0
for i in range(D):
  m+=s[i][t[i]-1]
  last_di[t[i]-1]=i+1
  for j in range(26):
    m-=c[j]*((i+1)-last_di[j])
  print(m)

Submission status: Accepted

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


ans = 0
last_date = [0]*26
for day in range(D):
  contest = t[day]-1
  ans += s[day][contest]
  last_date[contest] = day+1
  for j in range(26):
    ans -= c[j] * (day+1 - last_date[j])

  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]
out = [int(input()) for _ in range(D)]

sat = 0
last = [0] * 26
for d in range(D):
    assert(1 <= out[d] and out[d] <= 26)
    j = out[d] - 1
    last[j] = d + 1
    for i in range(26):
        sat -= (d + 1 - last[i]) * c[i]
    sat += s[d][j]
    print(sat)

Submission status: Accepted

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)]
cnt = [0]*26
ans = 0
for i in range(D):
  ans += s[i][t[i]-1]
  cnt[t[i]-1] = i+1
  for j in range(26):
     ans -= c[j] * (i+1-cnt[j])
  print(ans)

Submission status: Accepted

Solution code:


def main():
    d = int(input())
    c = [0] + list(map(int,input().split()))
    last = [0] * 27
    s = [0]
    for i in range(d):
        s.append([0] + list(map(int,input().split())))
    total = 0
    for j in range(1,d+1):
        t = int(input())
        last[t] = j
        total += s[j][t]
        for k in range(1,27):
            total -= c[k] * (j - last[k])
        print(total)






if __name__ == '__main__':
    main()

Submission status: Accepted

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 = [int(input()) for _ in range(D)]
def my_index(l,x,d,default=False):
    if x in l:
        return l.index(x)
    else:
        return d
def last(d,i):
    a= t[:d]
    a.reverse()
    day = -1*(my_index(a,i,d))+d
    return day
score = 0
for d in range(1,D+1):
    score += s[d-1][t[d-1]-1]
    for i in range(1,27):
        score -= c[i-1]*(d-last(d,i))
    print(score)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

def down(i,t):
    max = 0
    for k in range(26):
        max += c[k+1]*(i-last[k+1])
    return max

def manzoku(i,t):
    return s[i][t-1]-down(i+1,t)

last = { 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0,
         8:0, 9:0, 10:0, 11:0, 12:0, 13:0, 14:0, 15:0,
        16:0, 17:0, 18:0, 19:0, 20:0, 21:0, 22:0, 23:0,24:0, 25:0, 26:0
        }

# 日数
D = int(input())

line = input().split(" ")
c = [0]
for i in range(26):
    c.append(int(line[i]))

s = []
for i in range(D):
    line = input().split(" ")
    sSub = []
    for j in range(26):
        sSub.append(int(line[j]))
    s.append(sSub)

m = 0
for i in range(D):
    t = int(input())
    last[t] = i+1
    m += manzoku(i,t)
    print(m)

Submission status: Accepted

Solution code:
#import random
import numpy as np
d=int(input())
c=np.array(list(map(int,input().split())))
s=[None]*d
r0=[None]*d
r1=[None]*d
cm=np.array([0]*26)
cd=np.array([0]*26)
t=[None]*d
for i in range(d):
  s[i]=list(map(int,input().split()))
s=np.array(s)
for i in range(d):
  t[i]=int(input())-1
"""
for j in range(d):  
  #rand = np.random.randint(26, size = 10) 
  #print(np.argmax(s[j])+1)
  r_i=np.argmax(s[j])
  cd=c*cm
  if np.max(cd>s[i][r_i]):
    r_i=np.argmax(cd)
  r0[j]=r_i
  for k in range(26):
    cm[k]+=1
  cm[r_i]=0
  #r_i+=1
  #print(r_i)
cm=np.array([0]*26)
cd=np.array([0]*26)  
rand = np.random.randint(100, size = d) 
for j in range(d):  
  #print(np.argmax(s[j])+1)
  r_i=r0[j]
  cd=c*cm
  if rand[j]>80:
    r_i=np.argmax(cd)
  r1[j]=r_i
  for k in range(26):
    cm[k]+=1
  cm[r_i]=0
"""
r0=t
score=0
cm=np.array([0]*26)
cd=np.array([0]*26)  
for j in range(d):
  r0_j=r0[j]
  score+=s[j][r0_j]
  for k in range(26):
    cm[k]+=1
    cm[r0_j]=0
  cd=c*cm
  score+=-np.sum(cd)
  print(score)

Submission status: Accepted

Solution code:
D = int(input())
c = list(map(int,input().split()))
S = []
for _ in range(D):
  line = list(map(int,input().split()))
  S.append(line)
T = []
for _ in range(D):
  t = int(input())
  T.append(t)
dic = {n+1:0 for n in range(26)}
ans = 0
for i in range(1,D+1):
  dic[T[i-1]] = i
  ans += S[i-1][T[i-1]-1]
  for j in range(26):
    ans -= c[j]*(i - dic[j+1])
  print(ans)

Submission status: Accepted

Solution code:
import random
import sys

import numpy as np

global NN
NN = 26

readline = sys.stdin.buffer.readline


class Contest:
    """
    d: ある日付
    i: テストの種類
    self.day: 最後にテストiを開催した日
    """

    def __init__(self, c, s, D):
        self.last = [-1 for _ in range(NN)]
        self.C = c
        self.S = s
        self.score = 0

    def minus_score(self, d, i):
        m = self.C[i] * (d - self.last[i])
        return m

    def sum_minus_scores(self, d):
        m = 0
        for i in range(NN):
            m += self.minus_score(d, i)

        return m

    # テストiを開催したときのスコア
    def update_score(self, i, d):
        self.last[i] = d
        self.score += S[d][i]
        self.score -= self.sum_minus_scores(d)
        return self.score

    # 日付を更新せずにスコアを計算
    def calc_score(self, i, d):
        s = self.score
        last_d = self.last[i]
        self.last = d
        s += S[d][i]
        s -= self.sum_minus_scores(d)
        self.last = last_d

        return s


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

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

tests = [i - 1 for i in tests]

T = Contest(C, S, D)

for d, test in enumerate(tests):
    print(T.update_score(test, d))

Submission status: Accepted

Solution code:
#Aの入力

D=int(input())

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

s = []#Dx26の行列
for i in range(D):
    s += [list(map(int, input().split()))]

t = [int(input()) for i in range(D)]#Aの出力 (コンテスト名　0~25) len = D

last = [0 for i in range(len(c))]#最後に行われた日

value = 0

v = []#len = 26

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])
    v += [value] 


#Bの出力
for i in range(D):
    print(v[i])

Submission status: Accepted

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

last = [0] * 27
manzoku = [0] * 27
for i in range(1, D+1):
    t = int(input())
    manzoku[t] += s[i-1][t-1]
    last[t] = i
    for j in range(1, 27):
        manzoku[j] -= c[j] * (i - last[j])
            
    result = 0
    for k in range(27):
        result += manzoku[k]
    print(result)

Submission status: Accepted

Solution code:
import numpy as np

D = int(input())
c = np.array( list(map(int, input().split())) )
s = [[] for i in range(365+1)]
for i in range(D):
  s[i] = np.array( list(map(int, input().split())) )
#
last = np.array( [-1]*26 )
av = np.array( [0]*26 )
id = np.identity(4,dtype=int)

v = 0
for d in range(D):
  av = s[d] - sum( c*(d-last) ) + c*(d-last) 
  t =  int(input())
  t -= 1
  last[t] = d
  v += av[t]
  print( v )
#

Submission status: Accepted

Solution code:
from collections import Counter
D = int(input())
c = list(map(int, input().split()))
s = []
for i in range(D):
    temp_s = list(map(int, input().split()))
    s.append(temp_s)
my_count = Counter()
t = list(int(input()) for i in range(D))
c_sum = sum(c)
my_sum = 0
my_minus = 0
for d in range(D):
    my_count [t[d]] = c[t[d] - 1] * (d + 1)
    my_sum += s[d][t[d] - 1]
    my_minus += sum(dict(my_count).values()) - c_sum * (d + 1)
    print(my_sum + my_minus)

Submission status: Accepted

Solution code:
def resolve():
    import numpy as np
    d = int(input())
    C = list(map(int, input().split()))
    S = []
    for _ in range(d):
        _S = list(map(int, input().split()))
        S.append(_S)
    S = np.array(S)

    T = []
    for _ in range(d):
        T.append(int(input()))

    last = [-1 for i in range(26)]
    score = 0
    for i in range(d):
        # max_idx = np.argmax(S[i])
        # print(max_idx + 1)
        score += S[i][T[i] - 1]
        last[T[i] - 1] = i
        for j in range(26):
            score -= C[j] * (i - last[j])

        print(score)

resolve()

Submission status: Accepted

Solution code:
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]
out = [int(input()) for _ in range(D)]
last = [0] * 26
sat = 0

for d in range(D):
    j = out[d] - 1
    last[j] = d + 1
    for i in range(26):
        sat -= (d + 1 - last[i]) * c[i]
    sat += s[d][j]
    print(sat)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
D = int(input())
C = [int(x) for x in input().split()]
S = []
for i in range(D):
    S.append([int(x) for x in input().split()])
T = [int(input()) for i in range(D)]


def last(d, i):
    l = 0
    for j in range(1, d + 1):
        if T[j - 1] == i + 1:
            l = j
    return l


score = 0

for d in range(1, 1 + D):
    score += S[d - 1][T[d - 1] - 1] - \
        sum([C[i]*(d - last(d, i)) for i in range(26)])
    print(score)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from typing import NamedTuple, List

class Game(NamedTuple):
    c: List[int]
    s: List[List[int]]

def solve(D: int, c: "List[int]", s: "List[List[int]]", t: "List[int]"):
    from functools import reduce
    ans = [0]
    lasts = [0] * 26
    adjust = 0
    sum_c = sum(c)
    daily_loss = sum_c
    for day, tt in enumerate(t, 1):
        a = s[day-1][tt-1]
        adjust += day * c[tt-1] - lasts[tt-1] * c[tt-1]
        lasts[tt-1] = day        
        a -= daily_loss
        a += adjust
        ans.append(ans[-1]+a)
        daily_loss += sum_c
    return ans[1:]

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    D = int(next(tokens))  # type: int
    c = [int(next(tokens)) for _ in range(26)]  # type: "List[int]"
    s = [[int(next(tokens)) for _ in range(26)] for _ in range(D)]  # type: "List[List[int]]"
    t = [int(next(tokens)) for _ in range(D)]  # type: "List[int]"
    print(*solve(D, c, s, t), sep="\n")

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Accepted

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)]
v=0
LD=[0]*26

for i in range(D):
  v+=s[i][t[i]-1]
  LD[t[i]-1]=i+1  
  for j in range(26):
    v-=c[j]*(i+1-LD[j])
  print(v)

Submission status: Accepted

Solution code:
D = int(input())
s = [input() for i in range(2*D+1)]
for i in range(0,2*D+1):
    s[i] = s[i].split()
    s[i] = [int(p) for p in s[i]]
a = []
b = [0]*len(s[0])
S = 0
m = 0
for i in range (1,D+1):
    for j in range(0,len(s[0])):
        if j == s[D+i][0]-1:
            b[s[D+i][0]-1] = 0
        else:
            b[j] = b[j] + s[0][j]
    S = S + s[i][s[D+i][0]-1] - sum(b)
    a.append(S)
for i in (a):
    print(i)

Submission status: Accepted

Solution code:
def resolve():
	D = int(input())
	# 各コンテストタイプの満足度の下がりやすさ
	c = list(map(int, input().split()))
	# 各日程に各コンテストを開催した時の満足度増加量
	s = []
	for i in range(D):
		s.append(list(map(int, input().split())))
	# 最後に各コンテストを開催した日
	l = [-1] * 26
	# 満足度
	v = 0
	for d in range(D):
		# この日に開催されるコンテストタイプ
		t = int(input())-1
		l[t] = d
		# 増加分加算
		v += s[d][t]
		# 不満足分減算
		for i in range(26):
			v -= c[i] * (d - l[i])
		print(v)
resolve()

Submission status: Accepted

Solution code:
D=int(input())
c=list(map(int,input().split()))
c.insert(0,0)
s=[0]*(D+1)
t=[0]*(D+1)
for i in range(1,D+1):
	s[i]=list(map(int,input().split()))
for i in range(1,D+1):
	t[i]=int(input())

def last(d,i):
	for j in range(d, 0, -1):
		if t[j]==i: return j
	return 0

S=0
for d in range(1, D+1):
	cd = sum((c[i] * (d - last(d, i)) for i in range(1, 27)))
	S += s[d][t[d]-1] - cd
	print(S)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from decimal import *

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	D = inputInt()
	C = inputList()
	S = []
	ans = []
	for i in range(D):
		s = inputList()
		S.append(s)

	for i in range(D):
		tmp = inputInt()
		ans.append(tmp)

	scezhu = [inf for i in range(26)]
	sco = 0
	for i,val in enumerate(ans):
		tmp = S[i][val-1]
		scezhu[val-1] = i
		mins = 0
		for j,vol in enumerate(C):
			if scezhu[j] == inf:
				mins = mins + (vol * (i+1))
				#if i == 1:
					#print(vol * (i+1))
			else:
				mins = mins + (vol * ((i+1)-(scezhu[j]+1)))
				#if i == 1:
					#print(scezhu[i]+1)
		#print(mins)
		tmp -= mins
		sco += tmp
		print(sco)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()