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.

Solution (Python):

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):  
  
  
  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
  
  
cm=np.array([0]*26)
cd=np.array([0]*26)  
rand = np.random.randint(100, size = d) 
for j in range(d):  
  
  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)

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

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

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

Solution (Python):


D=int(input())

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

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

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

last = [0 for i in range(len(c))]

value = 0

v = []

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] 



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

Solution (Python):

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)

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

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

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

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

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

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

Solution (Python):

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


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    D = int(next(tokens))  
    c = [int(next(tokens)) for _ in range(26)]  
    s = [[int(next(tokens)) for _ in range(26)] for _ in range(D)]  
    t = [int(next(tokens)) for _ in range(D)]  
    print(*solve(D, c, s, t), sep="\n")

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

if __name__ == '__main__':
    
    main()

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

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

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

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

Solution (Python):

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))
				
					
			else:
				mins = mins + (vol * ((i+1)-(scezhu[j]+1)))
				
					
		
		tmp -= mins
		sco += tmp
		print(sco)



if __name__ == "__main__":
	main()

Solution (Python):
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 range(d):
    t = int(input())-1
    day[t] = num
    for j in range(26):
        if j == t:
            ans += s[i][j]
        else:
            ans -= c[j]*(num-day[j])
    print(ans)
    num += 1

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

last = [0]*26
ans = 0
for d in range(D):
    max_s = int(input()) - 1
    last[max_s] = d+1
    ans += s[d][max_s]
    for i in range(26):
        ans -= (c[i]*(d+1 - last[i]))
    print(ans)

Solution (Python):
def data_input():
    D = int(input())
    C = tuple(map(int, input().split()))

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

    return D, C, S


def score(D, C, S, T):
    """入力D, C, Sと出力Tからスコアを計算する関数"""

    last = [0] * 26  

    def decrease(d, last):
        """d日めにiを開催した時の満足度の減少を返す関数"""
        res = 0
        for i in range(26):
            ith = C[i] * (d+1 - last[i])
            res -= ith
        return res

    V = []  
    for d, i in enumerate(T):
        if d == 0:
            v = 0
        last[i] = d + 1
        v += S[d][i] + decrease(d, last)
        V.append(v)
    return V

D, C, S = data_input()

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

V = score(D, C, S, T)

for v in V:
    print(v)

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
from decimal import Decimal
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

D = I()
C = LI()
s = [LI() for _ in range(D)]

T = [I() for _ in range(D)]



def last(d,i):
    T_d = T[:d]
    n = [j for j, x in enumerate(T_d,1) if x == i]
    if n:
        
        return max(n)
    else:
        return 0
        
        
score = 0
for d in range(1,D+1):
    sum_c = 0
    for i in range(1,26+1):
        sum_c += C[i-1] * (d -last(d,i))
    score += s[d-1][T[d-1]-1] - sum_c
    print(score)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
import numpy as np

D = int(input())
c = np.array(input().split(), dtype=np.int32)
s = np.array([input().split() for _ in range(D)], dtype=np.int32)
t = [int(input()) for _ in range(D)]
last = np.zeros(26, dtype=np.int32)



ans = []
point = 0
for i in range(D):
    last[t[i]-1] = i+1
    down = (i+1-last)*c
    point += s[i, t[i]-1] - down.sum()
    print(point)

Solution (Python):
D = int(input())
c = [int(x) for x in input().split()]
s = [input().split() for l in range(D)]
t = []
for i in range(D):
    t += [int(input())]
manzoku = 0
d = 1
last = [0]*26

for p in range(D):
    
    manzoku += int(s[p][t[p]-1])
    last[t[p]-1] = p+1
    for i in range(26):
        manzoku -= c[i] *(p+1-last[i])
    print(manzoku)

Solution (Python):
import sys
import math
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate 
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache 
def choice_contest(c, data_day, last, day, contest):
    contest -= 1
    
    last = [i + 1 for i in last]
    
    last[contest] = 0
    
    
    
    d_satis = sum([last[i] * c[i] for i in range(26)])
    
    return [int(data_day[contest]) - d_satis, last]

def main():
    d = int(input())
    c = list(map(int,input().split()))
    data = [input().split() for _ in range(d)]
    last = [0] * 26
    ans = 0
    for i in range(d):
        s, last = choice_contest(c, data[i], last, i, contest = int(input()))
        ans += s
        print(ans)
    return 0

if __name__ == "__main__":
    main()

Solution (Python):

def cost(c, d, lastdi):
    total = 0
    
    
    for i in range(26):
        total += c[i]*(d - lastdi[i])
    
    return total


D = int(input())

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

s = [[] for _ in range(D)]
for d in range(D):
    si = list(map(int, input().split()))
    s[d] = si


lastdi = [0 for _ in range(26)]

ans = 0
for d in range(D):
    
    typei = int(input()) - 1
    lastdi[typei] = d+1 
    ans += s[d][typei] - cost(c, d+1, lastdi)
    print(ans)

Solution (Python):
import numpy as np

D=int(input())
c=[int(i) for i in input().split()]
s=[]
for d in range(D):
    s.append([int(i) for i in input().split()])

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

v = [0]*D
last = [[0 for i in range(26)] for d in range(D)]

for d in range(D):
    if d>=1:
        last[d] = last[d-1][:]
    last[d][t[d]] = d+1

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

v_sum=0
for d in range(D):
    v_sum+=v[d]
    print(v_sum)

Solution (Python):
D = int(input())
c_list = list(map(int,input().split(' ')))
s_list = []
for i in range(D):
    s_list.append(list(map(int,input().split(' '))))
t_list = []
for j in range(D):
    t_list.append(int(input()))
last_list = [0 for k in range(26)]
score = 0
point = 0
for day in range(D):
    point += s_list[day][t_list[day]-1]
    last_list[t_list[day]-1] = day+1
    for c in range(26):
        point -= c_list[c]*((day+1)-last_list[c])
    if(point>-1000000):
        score = point+1000000
    print(point)

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

def last(d,la):
    s = 0
    for i in range(26):
        s += c[i] * (d - la[i])
    return s

lastday = [0]*26
a = 0
for d in range(D):
    lastday[t[d]-1] = d + 1
    a += s[d][t[d]-1]- last(d+1,lastday)
    print(a)

Solution (Python):
D=int(input())
c=list(map(int,input().split()))
s=[]
for _ in range(D):
  s.append(list(map(int,input().split())))
t=[]
for _ in range(D):
  t.append(int(input()))
g=0
for d in range(D):
  a=s[d][t[d]-1]
  b=0
  for i in range(26):
    for j in range(d+1,0,-1):
      if t[j-1]==i+1:
        b+=c[i]*(d+1-j)
        break
    else:
      b+=c[i]*(d+1)
  g+=a-b
  print(g)

Solution (Python):
D = int(input())
c = list(map(int,input().rstrip().split(" ")))
s = []
t = []
kankaku = [0 for i in range(27)]
last = [0 for i in range(27)]
man = 0
for i in range(D):
    s.append(list(map(int,input().rstrip().split(" "))))
for i in range(D):
    t.append(int(input())-1)
for i in range(D):
    man += s[i][t[i]]
    last[t[i]] = i + 1
    for i2 in range(26):
        man -= (i + 1 - last[i2]) * c[i2]
    print(man)

Solution (Python):
D=int(input())
c = [int(i) for i in input().split()]
s=[]
for i in range(D):
    s.append([int(j) for j in input().split()])
t=[0] * D
for i in range(D):
    t[i] = int(input())

tst = [0] * 26
last = [0] * 26
for i,j in enumerate(t):
    tst[j-1] += s[i][j-1]
    last[j-1] = i+1
    for k in range(26):
        if k != j-1:
            tst[k] -= (i+1 - last[k])*c[k] 
    print(sum(tst))

Solution (Python):
d = int(input())
ci = list(map(int, input().split()))
sdi_list = []
for i in range(d):
    sdi = list(map(int, input().split()))
    sdi_list.append(sdi)
td_list = []
for i in range(d):
    td = int(input())
    td_list.append(td)
lastdi = [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]
manzoku = 0
for i in range(d):
    manzokuteika = 0
    lastdi[td_list[i]-1] = i + 1
    for j in range(26):
        manzokuteika = manzokuteika + ci[j] * (i + 1 - lastdi[j])
    manzoku = manzoku + sdi_list[i][td_list[i]-1] - manzokuteika
    print(manzoku)

Solution (Python):
d =int(input())
c = list(map(int, input().split())) 
s = []
t = []

d_l = []
score = 0
soft = 0

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

for i in range(26):
    d_l.append(0)
    
for i in range(d):
    t = int(input())
    soft = 0
    
    for j in range(26):
        if t-1 == j:
            d_l[j] = 0
            score += s[i][j]
        else:
            d_l[j] += 1
            soft += c[j] * d_l[j]
    
    score -= soft
    print(score)

Solution (Python):
from operator import mul

D=int(input())
clist=list(map(int,input().split()))
slist=[]
for i in range (D):
    s=list(map(int,input().split()))
    slist.append(s)
tlist=[]
for i in range(D):
    t=int(input())
    tlist.append(t)







noheldsday=[0]*26

benefit=0

for i in range (D):
    _noheldsday=list(map(lambda x: x+1,noheldsday))
    _noheldsday[tlist[i]-1]=0
    unpleasant=list(map(mul,clist,_noheldsday))
    
    v=slist[i][tlist[i]-1]-sum(unpleasant)
    benefit+=v
    noheldsday=_noheldsday
    
    print(benefit)

Solution (Python):
def complain(comp,c,k,last_hold,i):
    
    if(k > 26):
        
        return comp
    comp += c[k-1]*((i+1) - last_hold[k-1]) 
    k += 1
    
    return complain(comp,c,k,last_hold,i)

D = int(input())
c = list(map(int,input().split()))
s = ["_"]*(D)

for i in range(D):
    s[i] = list(map(int,input().split()))
last_hold = [0]*26
score = [0]*(D+1)

for i in range(D):
    score[i+1] += score[i]
    t = int(input())
    last_hold[t-1] = i+1
    comp = 0
    k = 1
    score[i+1] += s[i][t-1]    
    
    comp = complain(comp,c,k,last_hold,i)    
    score[i+1] -= comp
    
    

for i in range(D):
    print(score[i+1])

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

a = [0]*26
total = 0
for i in range(D):
    ts = t[i]

    a[ts-1] = i+1
    cs = sum([x * (i+1 - y) for x, y in zip(c, a)])

    total = total + s[i][ts-1] - cs
    print(total)

Solution (Python):
def f_last(d, i, lt):
  idx = []
  if i in lt[0:d]:
    idx = [j+1 for j, x in enumerate(lt[0:d]) if x == i]
  else:
    idx.append(0)
  return idx[-1]

d = int(input())
lc = list(map(int, input().split()))
ls = []
lt = []
for _ in range(d):
  ls.append(list(map(int, input().split())))

for _ in range(d):
  lt.append(int(input()))

v = 0
for dd in range(1, d+1):
  v_d = 0
  for i in range(len(lc)):
    v_d += lc[i]*(dd-f_last(dd, i+1, lt))
  v = v + ls[dd-1][lt[dd-1]-1] - v_d
  print(v)

Solution (Python):
D=int(input())
c=list(map(int,input().split()))
last=[0 for i in range(26)]
cost=[0 for i in range(26)]
degree=[0 for i in range(26)]
score=0
s=[]
t=[]
for _ in range(D):
  s.append(list(map(int,input().split())))
for _ in range(D):
  t.append(int(input()))
for d in range(D):
  for i in range(26):
    cost[i]+=c[i]*(d+1-last[i])
  k=t[d]-1
  cost[k]-=c[k]*(d+1-last[k])
  last[k]=d+1
  score+=s[d][k]
  print(score-sum(cost))

Solution (Python):




















D = int(input())
C = [int(i) for i in input().split()]

SS = [[int(i) for i in input().split()] for _ in range(D)]
T = [int(input()) for _ in range(D)]

L = [0]*26

v = 0
for d, t in enumerate(T):
  t -= 1
  L[t] = d+1

  v += SS[d][t]
  for c, l in zip(C, L):
    v -= c * (d+1 - l)
  

  print(v)

Solution (Python):
D = int(input())

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

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

L = [0 for i in range(26)]

T = [int(input()) for i in range(D)]

score=0

for d in range(D):
  score += S[d][T[d]-1]
  
  L[T[d]-1] = d+1
  down = 0
  for i in range(26):
    down+=C[i] * ((d+1) - L[i])

  score -=down
  
  print(score)

Solution (Python):
def main():
    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()))

    t = [0 for i in range(d)]
    for i in range(d):
        t[i] = int(input())

    ans = 0
    last = [0 for i in range(26)]

        
    for i in range(d):
        ans += s[i][t[i]-1]
        last[t[i]-1] = i+1

        sum_ = 0
        for j in range(26):
            sum_ += c[j] * (i+1-last[j])

        ans -= sum_
        print(ans)
        
main()

Solution (Python):
d=int(input())
c=list(map(int,input().split()))
s=[list(map(int,input().split())) for _ in range(d)]
C=sum(c)
tot=0
mem=[0]*26
for i in range(d):
	t=int(input())
	mem[t-1]=i+1
	tot+=s[i][t-1]
	for j in range(26):
		tot-=c[j]*((i+1)-mem[j])
	print(tot)

Solution (Python):
D = int(input())
cs = list(map(int, input().split()))
ss = [input().split() for l in range(D)]
ts = [int(input()) for i in range(D)]
ans=0
all_cs=sum(cs)
cs_last=[0]*26

def c0(d):
    mainas=0
    for i in range(26):
        
        mainas-=cs[i]*(d-cs_last[i])
    return mainas



for i in range(D):
    cs_last[ts[i]-1]=i+1
    ans+=int(ss[i][ts[i]-1])+c0(i+1)
    
    print(ans)

Solution (Python):
def resolve():
    D = int(input())
    C = [int(x) for x in input().split()]
    S = []
    for i in range(D):
        S.append(list(map(int, input().split())))
    t = [int(input()) for _ in range(D)]
    last_d = []
    score = 0

    for d in range(D):
        score += S[d][t[d] - 1]
        last_d.append(t[d] - 1)
        for i in range(len(C)):
            if i in last_d:
                score -= C[i] * last_d[::-1].index(i)
            else:
                score -= C[i] * (d + 1)
        print(score)


if __name__ == "__main__":
    resolve()

Solution (Python):
D = int(input())
c = list(map(int, input().split()))
s = []
for d in range(D):
	s.append(list(map(int, input().split())))
last = [0 for i in range(26)]
v = 0
for d in range(1, D+1):
  i = int(input())
  last[i-1] = d
  v += s[d-1][i-1]
  for k in range(26):
    v -= c[k] * (d - last[k])
  print(v)

Solution (Python):



import sys


def input():
    return sys.stdin.readline()


def down_pleasure(c_list, day_num, contest_last_day):
    """その日の満足度低下を返す"""
    sum_down_pl = 0
    for i in range(26):
        sum_down_pl += c_list[i] * (day_num - contest_last_day[i])

    return sum_down_pl


def calc_pleasure(s_list, t_list, d, c_list, contest_last_day):
    pleasure = 0

    for day, t_num in zip(range(d), t_list):
        pleasure += s_list[day][t_num-1]
        contest_last_day[t_num-1] = day + 1
        pleasure -= down_pleasure(c_list, day+1, contest_last_day)
        print(pleasure)

    return pleasure






def resolve():
    d = int(input().rstrip())
    c_list = list(map(int, input().split()))
    s_list = [list(map(int, input().split())) for i in range(d)]
    t_list = []

    for i in range(d):
        t_list.append(int(input().rstrip()))

    contest_last_day = []
    for i in range(26):
        contest_last_day.append(0)

    pleasure = calc_pleasure(s_list, t_list, d, c_list, contest_last_day)




if __name__ == "__main__":
    resolve()

Solution (Python):
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)]
v = 0
last = [0 for _ in range(len(c))]

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)

Solution (Python):


D = int(input())
c = list(map(int, input().split()))
s = []
t = []

for i in range(D):
    s.append(list(map(int, input().split())))

for i in range(D):
    t.append(int(input()) - 1)

last = [0] * 26

ans = 0

for i in range(D):
    ans = ans + s[i][t[i]]
    for j in range(26):
        if t[i] != j:
            last[j] += 1
            ans = ans - (c[j] * last[j])
        elif t[i] == j:
            last[j] = 0
    print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline

    d = int(input())
    c = list(map(int, input().split()))
    s = [list(map(int, input().split())) for _ in range(d)]
    t = [int(input()) for _ in range(d)]
    last = [0 for _ in range(26)]
    score = 0

    for di in range(1, d + 1):
        ti = t[di - 1]
        score += s[di - 1][ti - 1]

        last[ti - 1] = di

        for j, cj in enumerate(c, 1):
            score -= cj * (di - last[j - 1])

        print(score)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
d = int(sys.stdin.readline().rstrip())
list_c = list(map(int, sys.stdin.readline().rstrip().split()))
list_s = []
list_t = []
need = 0
for i in range(d):
  list_s.append(list(map(int, sys.stdin.readline().rstrip().split())))
for i in range(1, d+1):
  t = int(sys.stdin.readline().rstrip())
  list_t.append(t)
  need += list_s[i-1][t-1]
  MINUS = 0
  list_t1 = list_t[::-1]
  for j in range(1, 27):
    if not j in list_t1:
      MINUS += list_c[j-1] * len(list_t)
    else:
      MINUS += list_c[j-1] * list_t1.index(j)
  need -= MINUS
  print(need)

Solution (Python):
import numpy as np

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

last_day = np.zeros(26, dtype=int)

score = 0
for i in range(1, d + 1):
    last_day[t[i - 1] - 1] = i
    minus = ((last_day - i) * c).sum()
    score += s[i - 1, t[i - 1] - 1] + minus
    print(score)

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

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

def dissat(day):
  dissat = 0
  for i in range(26):
    if T[day-1]-1 == i:
      continue
    dissat += C[i]*(day-lastdays[i])

  return dissat    
    
def last(day):
  lastdays[T[day-1]-1] = day

sat = 0
lastdays = [0]*26


for i in range(D):
  
  last(i)
  sat += S[i][T[i]-1] - dissat(i+1)
  print(sat)

Solution (Python):
import random
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)]

A = [0]*26

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