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 sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

D = ini()
C = inl()
assert len(C) == 26
S = [None] * D
for i in range(D):
    S[i] = inl()
    assert len(S[i]) == 26
T = [ini() - 1 for _ in range(D)]


def solve():
    ans = 0
    last = [-1] * 26
    for d in range(D):
        ct = T[d]
        ans += S[d][ct]
        last[ct] = d
        for i in range(26):
            ans -= C[i] * (d - last[i])
        print(ans)


solve()

Solution (Python):
N =26

D = int(input())
c = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(0,D,1)]
t = [int(input())-1 for i in range(0,D,1)]

last = [-1]*(N)
manzoku = 0

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

Solution (Python):
NTEST = 26

D=int(input())

C=list(map(int,input().split()))
lastDay = [0 for i in range(NTEST)]

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

tests=list()
tests.append(0)
for d in range(D):
    tests.append(int(input()))

sat = 0
for d in range(1,D+1):
    tst = tests[d] - 1
    lastDay[tst] = d
    sat += SL[d][tst]
    for itst in range(NTEST):
        sat -= C[itst] * (d-lastDay[itst])
    
    print(sat)

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

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

ans = 0

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

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

Solution (Python):
def resolve():
    D = int(input())
    cost = list(map(int, input().split()))
    sat = [list((map(int, input().split()))) for _ in range(D)]
    last = [0]*26

    sumsat = 0

    for i in range(D):
        pro = int(input())
        pro -= 1
        last[pro] = i + 1
        plussat = sat[i][pro]
        minussat = 0
        for j in range(26):
            minussat += (i+1-last[j])*(cost[j])
        sumsat += (plussat-minussat)

        print(sumsat)
resolve()

Solution (Python):
import numpy as np


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

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

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

ans = []
for d, t in enumerate(T, 1):
    last_day[t-1] = d
    decrease = c * (d - last_day)
    score += S[d-1, t-1] - decrease.sum()
    ans.append(score)

print(*ans, sep='\n')

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    D = int(readline())
    C = [int(i) for i in readline().split()]
    S = [[int(i) for i in readline().split()] for _ in range(D)]
    T = [int(readline()) for _ in range(D)]

    last = [0] * 26
    score = 0
    for i, t in enumerate(T):
        score += S[i][t-1]
        last[t-1] = i+1
        for j, c in enumerate(C):
            score -= c * (i+1 - last[j])

        print(score)

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)]
sumc = sum(C)
last = [0 for i in range(26)]

ans = 0
for i in range(D):
    t = int(input())
    last[t-1] = i+1
    ans += S[i][t-1]
    for j in range(26):
        ans -= C[j]*(i+1 - last[j])
    print(ans)

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

N = 26
scores = [0] * N
last = [-1] * N
ans = []
for d in range(D):
    last[t[d]] = d
    scores[t[d]] += s[d][t[d]]

    for i in range(N):
        if last[t[d]] != -1:
            scores[i] -= c[i] * (d - last[i])
    ans.append(sum(scores))

print(*ans, sep="\n")

Solution (Python):
import numpy as np

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

last = np.zeros(26,int)

score = 0

for i in range(D):
    t = int(input())
    t -= 1
    score += s[i][t]
    last += 1 
    last[t] = 0
    score -= np.dot(c,last)
    
    print(score)

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

Solution (Python):
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)
held=[0 for _ in range(26)]
ans=0
for d in range(D):
    s=S[d]
    place=T[d]
    held[place]=d+1
    ans+=s[place]
    for n in range(26):
        ans-=c[n]*(d+1-held[n])
    print(ans)

Solution (Python):
d = int(input())
c_l = list(map(int,input().split()))
s_l = [list(map(int,input().split())) for i in range(d)]

t_l = [int(input()) for i in range(d)]

last_l = [-1 for i in range(26)]

ans = 0
for day in range(d):
  contest_num = t_l[day]
  contest_num -= 1

  contest_val = s_l[day][contest_num]

  ans += contest_val
  last_l[contest_num] = day

  for i in range(26):
    ans -= c_l[i] * (day - last_l[i])

  print(ans)

Solution (Python):
import sys
def input():
    return sys.stdin.readline().rstrip()
def main():
    def scoring(ans, d, t, C, S, L):
        ans += S[d][t]
        L[t] = d
        ans -= sum([c * (d - l) for (c, l) in zip(C, L)])        
        return ans, L
    D = int(input())
    C = list(map(int, input().split()))
    S = [list(map(int, input().split())) for _ in range(D)]
    L = [-1] * 26
    ans = 0
    for d in range(D):
        t = int(input()) - 1
        ans, L = scoring(ans, d, t, C, S, L)
        print(ans)


if __name__ == '__main__':
    main()

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

    d = int(readline())
    c = list(map(int, readline().split()))
    s = [list(map(int, readline().split())) for _ in range(d)]
    ans = 0
    memo = [0] * 26
    for i in range(1, d + 1):
        t = int(readline()) - 1
        ans += s[i - 1][t]
        memo[t] = i
        for m, cc in zip(memo, c):
            ans -= cc * (i - m)
        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

D = int(input())
C = list(map(int, sys.stdin.readline().rsplit()))
S = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(D)]

d = [0] * 26
res = 0
for i in range(1, D + 1):
    t = int(input()) - 1
    res += S[i - 1][t]
    d[t] = i
    for j, dd in enumerate(d):
        res -= C[j] * (i - dd)
    print(res)

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

Solution (Python):
D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for _ in range(D)]
E = [-1] * 26
score = 0
for i in range(D):
    t = int(input())
    score += S[i][t-1]
    E[t-1] = i
    for j in range(26):
        score -= C[j] * (i - E[j])
    print(score)

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

scores = []
last = [-1]*26
score = 0
for i in range(D):
    t = int(input())
    t -= 1
    score += S[i][t]
    last[t] = i
    for j in range(26):
        score -= C[j]*(i-last[j])
    scores.append(score)
print(*scores, sep='\n')

Solution (Python):
import numpy as np
d=int(input())
c=[0]+[int(j)for j in input().split()]
s=[[0]+[int(j)for j in input().split()]for i in range(d)]
t=[int(input())for i in range(d)]
l=[0]*27
ans=0
for i,j in enumerate(t,1):
    ans+=s[i-1][j]
    l[j]=i
    for k in range(1,27):
        ans-=(i-l[k])*c[k]
    print(ans)

Solution (Python):
import numpy as np

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

L = np.empty(26, int)
L[:] = -1

score = 0
for d in range(D):
    t = int(input())-1
    L[t] = d
    score += S[d, t] - np.dot(C, d-L)
    print(score)

Solution (Python):
D = int(input())
C = [int(c) for c in input().split()]
S = [[int(s) for s in input().split()] for _ in range(D)]
T = [int(input()) for _ in range(D)]
ans = 0
L = [0]*26
for i in range(D):
    ans += S[i][T[i]-1]
    L[T[i]-1] = i+1
    for j in range(26):
        ans -= C[j]*(i+1-L[j])
    print(ans)

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

Solution (Python):
from sys import stdin


def main():
    _in = [_.rstrip() for _ in stdin.readlines()]
    D = int(_in[0])  
    c_arr = list(map(int, _in[1].split(' ')))  
    s_arr = []
    for i in range(D):
        _ = list(map(int, _in[i + 2].split(' ')))  
        s_arr.append(_)
    t_arr = []
    for i in range(D):
        _ = int(_in[i + D + 2])  
        t_arr.append(_)
    
    v_arr = [None] * D
    v = 0
    blank = [0] * 26
    for i in range(D):
        t = t_arr[i] - 1
        blank = list(map(lambda x: x + 1, blank))
        blank[t] = 0
        v += s_arr[i][t_arr[i] - 1]
        for j, _blank in enumerate(blank):
            v -= c_arr[j] * _blank
        v_arr[i] = v
        
        print(v)


if __name__ == "__main__":
    main()

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

d = int(input())
C = tuple(map(int, input().split()))
S = tuple(tuple(map(int, input().split())) for _ in range(d))
ans = 0
L = [0]*26
for i in range(1, d+1):
  t = int(input())
  t -= 1
  L[t] = i
  ans += S[i-1][t]
  for j, (c, l) in enumerate(zip(C, L)):
    ans -= c*(i-l)
  print(ans)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

D = int(input())
c = [int(x) for x in input().split()]
s = []
for _ in range(D):
    s.append([int(x) for x in input().split()])
t = [int(input()) for x in range(D)]
last = [0]*26
ans = 0
for i in range(1, D + 1):
    ans += s[i - 1][t[i - 1] - 1]
    last[t[i - 1] - 1] = i
    for j in range(26):
        ans -= c[j]*(i - last[j])
    print(ans)

Solution (Python):
import numpy as np
D = int(input())
C = np.array(input().split(), dtype=int)

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

last = np.ones(26, dtype=int)


ans = 0
for d, t in enumerate(T):
    t -= 1
    last[t] = 0

    score = S[d][t]
    score -= (C * last).sum()
    ans += score
    print(ans)
    last += 1

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

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

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

Solution (Python):
d=int(input())
c=list(map(int,input().split()))
ss=[list(map(int,input().split())) for i in range(d)]
ts=[int(input())for i in range(d)]
ans=0

from collections import defaultdict
las=defaultdict(int)

for i in range(d):
    t=ts[i]-1
    ans+=ss[i][t]
    las[t]=i+1
    ans-=sum(c[j]*(i+1-las[j])for j in range(26))
    print(ans)

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

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

score=0
last=[0]*26 
for d in range(1,D+1): 
    contest=int(input().strip())
    score+=s[d-1][contest-1]
    last[contest-1]=d
    for i in range(26): 
        score-=c[i]*(d-last[i])
    print(score)

Solution (Python):

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)

    ans = 0

    last = [0] * 26
    for j in range(1,D+1):
        t = int(input())
        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]
        print(ans)

main()

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)]
MAX = 26

last = [0] * MAX

score = 0
for day, e in enumerate(t, 1):
    score += s[day-1][e-1]
    last[e-1] = day

    for ec, elast in zip(c, last):
        score -= ec * (day - elast)

    print(score)

Solution (Python):
from collections import deque
from functools import reduce

d = int(input())
cl = [int(i) for i in input().split()]

sm = []
for _ in range(d):
  sm.append([int(i) for i in input().split()])
sd = deque(sm)

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

hold = [0] * 26
satisfy = 0
for t, num in enumerate(tl):
  hold[num-1] = t + 1
  sl = sd.popleft()
  satisfy += sl[num - 1]
  satisfy -= sum([i * j for i, j in zip(cl, [t - j + 1 for j in hold])])
  print(satisfy)

Solution (Python):
import numpy as np

D=int(input())
c=np.array(list(map(int,input().split())))
s=[list(map(int,input().split())) for _ in range(D)]
last=np.array([0]*26)
t=[int(input()) for _ in range(D)]

v=0
for d in range(1,D+1):
    v+=s[d-1][t[d-1]-1]
    last[t[d-1]-1]=d
    v-=np.dot(c,d-last)
    print(v)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

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

    ret = [0] * (D + 1)
    before = [-1] * 26
    for i in range(D):
        t = int(input()) - 1
        ret[i] += S[i][t]
        before[t] = i
        for j,c in enumerate(C):
            ret[i] -= (i - before[j]) * c
        ret[i + 1] += ret[i]
    print('\n'.join(map(str,ret[:-1])))
if __name__ == '__main__':
    main()

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

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

last = [-1 for _ in range(26)]
satisfy = 0
for day, t in enumerate(T):
    last[t] = day
    loss = sum(c[i]*(day-last[i]) for i in range(26))
    satisfy += s[day][t] - loss
    print(satisfy)

Solution (Python):
D = int(input())
C = list(map(int,input().split()))
L = [0 for _ in range(26)]
S = []
for _ in range(D):
    S.append(list(map(int,input().split())))

def calc(ans, select, n):
    for i,l in enumerate(L):
        if i != select:
            ans -= C[i] * (n-L[i])
        else:
            L[i] = n
    return ans

V = []
ans = 0
for i in range(D):
    select = int(input())-1
    ans += S[i][select]
    ans = calc(ans, select, i+1)
    print(ans)

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

if __name__ == '__main__':
    main()

Solution (Python):
def sumDesire(C, d, last):
    sumA = 0
    for i in range(26):
        sumA += C[i] * (d - last[i])
    return sumA


def resolve():
    from collections import defaultdict
    D = int(input())
    C = [int(i) for i in input().split()]
    SD = [list(map(int, input().split())) for _ in range(D)]
    T = [int(input()) for _ in range(D)]

    last = defaultdict(int)
    ans = 0
    for i in range(D):
        ans += SD[i][T[i] - 1]
        last[T[i] - 1] = i + 1
        ans -= sumDesire(C, i + 1, last)
        print(ans)


resolve()

Solution (Python):
d=int(input())
c=list(map(int,input().split()))
s=[list(map(int,input().split())) for _ in range(d)]
last=[-1 for i in range(26)]
x=0
for day in range(d):
  type=int(input())
  last[type-1]=day
  x+=s[day][type-1]
  for j in range(1,27):
    x-=(day-last[j-1])*c[j-1]
  print(x)

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=int(input())-1
    T.append(t)


Last=[0]*26

ans=0
for i in range(d):
    t=T[i]
    temp=S[i][t]
    for j in range(26):
        if j==t:
            Last[j]=i+1
        else:
            temp-=C[j]*(i+1-Last[j])
    ans+=temp
    print(ans)

Solution (Python):
D = int(input())
C = [0] + list(map(int,input().split()))
ls_s = [[0]*27] + [[0] + list(map(int,input().split())) for _ in range(D)]
ls_t = [0] + [int(input()) for _ in range(D)]


Score = 0
ls_last = [0] * 27
for d in range(1,D+1):
  a = ls_t[d] 
  ls_last[a] = d
  Score += ls_s[d][a]
  for i in range(1,27):
    Score -= C[i] * (d - ls_last[i])
  print(Score)

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  

D = I()
c = [0] + LI()
s = [0] + [[0] + LI() for i in range(D)]
t = [0] + [I() for i in range(D)]

last = [0]*27
manzokudo = 0

for i in range(1,D+1):
    last[t[i]] = i
    manzokudo += s[i][t[i]]
    r = 0
    for j in range(1,27):
        r += c[j]*(i-last[j])
    manzokudo -= r
    print(manzokudo)

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

Solution (Python):
d = int(input())
c = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(d)]
ld = [0]*26
score = 0
def dec(day):
    ans = 0
    for i in range(26):
        ans += c[i]*(day-ld[i])
    return ans


for i in range(d):
    t = int(input())
    score+=s[i][t-1]
    ld[t-1]=i+1
    score-=dec(i+1)
    print(score)

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    
    D = int(readline())
    C = list(map(int, readline().split()))
    S = list(list(map(int, readline().split())) for _ in range(D))
    last = [0]*26
    v = 0

    for d in range(1,D+1):
        t = int(readline())
        last[t-1] = d
        v += S[d-1][t-1]
        for c, l in zip(C,last):
            v -= c*(d-l)
        print(v)


if __name__ == '__main__':
    main()

Solution (Python):
def calc(D, C, S, T):
    s = 0
    l = [-1] * len(C)
    for d, t in enumerate(T):
        t -= 1
        s += S[d][t]
        l[t] = d
        s -= sum(c * (d - l[i]) for i, c in enumerate(C))
        print(s)
    return s


def main():
    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)]
    calc(D, C, S, T)

main()

Solution (Python):
D = int(input())
c = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(D)]
last = [-1]*26
ans = 0
for i in range(D):
  t = int(input())
  ans += s[i][t-1]
  last[t-1] = i
  ans -= sum([c[j]*(i-last[j]) for j in range(26)])
  print(ans)

Solution (Python):


import sys
read = sys.stdin.read
readline = sys.stdin.readline

D, = map(int,readline().split())

*c, = map(int,readline().split())
s = tuple(tuple(map(int,readline().split())) for _ in range(D))

def output(ans):
    for i in ans: print(i+1)

def score(c,s,ans):
    last = [-1]*26
    score = 0
    for i, ai in enumerate(ans):
        score += s[i][ai]
        last[ai] = i
        for ci,li in zip(c,last):
            score -= ci*(i-li)
        print(score)
    


ans = [*map(lambda x: int(x)-1,read().split())]

score(c,s,ans)

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 = []
for i in range(d):
    t.append(int(input())-1)

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

D = int(input())
Cs = list(mapint())
Ss = [list(mapint()) for _ in range(D)]
Ts = [int(input()) for _ in range(D)]
last = [0]*26

sati = 0
for i in range(D):
    t = Ts[i]-1
    sati += Ss[i][t]
    last[t] = i+1
    for j, l in enumerate(last):
        c = Cs[j]
        sati -= (i+1-l)*c
    print(sati)

Solution (Python):

def resolve():
    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 i, t in enumerate(T):
        score += S[i][t-1]
        last[t-1] = i+1
        for j in range(26):
            score -= C[j]*(i+1-last[j])
        print(score)



if __name__ == '__main__':
    resolve()

Solution (Python):
def examA():
    N = 26
    D = I()
    C = LI()
    S = [LI()for _ in range(N)]
    ans = [0]*D

    for v in ans:
        print(v)
    return

def examB():
    N = 26
    D = I()
    C = LI()
    S = [LI()for _ in range(D)]
    T = [I()for _ in range(D)]
    ans = [0]*D
    used = [0]*N
    for i,t in enumerate(T):
        ans[i] += ans[i-1]
        t -= 1
        ans[i] += S[i][t]
        used[t] = i+1
        for j in range(N):
            ans[i] -= C[j]*(i+1-used[j])
    for v in ans:
        print(v)
    return

def examC():
    ans = 0
    print(ans)
    return


from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}

getcontext().prec = 28

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examB()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Solution (Python):
d = int(input())
c = [int(i) for i in input().split()]
s = [[int(i) for i in input().split()] for j in range(d)]

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

ans = 0
last = [0]*26

for i in range(1,d+1):
    ans += s[i-1][t[i-1]-1]
    last[t[i-1]-1] = i
    for j in range(26):
        ans -= (i-last[j])*c[j]
    print(ans)

Solution (Python):




import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

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

    d=int(readline())
    c=list(map(int, readline().rstrip().split()))
    s=[0]*d
    for i in range(d):
        s[i]=list(map(int, readline().rstrip().split()))
    last=[-1]*26
    score=0
    for i in range(d):
        t=int(readline())-1
        last[t]=i
        score+=s[i][t]
        for j in range(26):
            score-=(i-last[j])*c[j]
        print(score)

    return

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

Solution (Python):


def read_input():
    d = int(input())
    c = list(map(int, input().split()))
    s = [list(map(int, input().split())) for _ in range(d)]
    return d, c, s


def simulate(d, c, s, t):
    """
    t : コンテスト番号列
    """
    last = {i: 0 for i in range(26)}

    def day_loss(curr):
        loss = 0
        for i, e in enumerate(c):
            loss += e * (curr - last[i])
        return loss
    
    sufficient = 0
    hist = []
    for day in range(1, d + 1):
        contest = t[day - 1] - 1
        last[contest] = day

        sufficient += s[day - 1][contest]
        sufficient -= day_loss(day)    

        hist.append(sufficient)

    return sufficient, hist


def submit():
    d, c, s = read_input()
    t = [int(input()) for _ in range(d)]
    _, hist = simulate(d, c, s, t)
    for h in hist:
        print(h)


if __name__ == "__main__":
    submit()

Solution (Python):


import sys
input=sys.stdin.readline

def main():
    D=int(input())
    C=list(map(int,input().split()))
    S=[list(map(int,input().split())) for i in range(D)]
    T=[[0]]+[int(input()) for i in range(D)]
    last=[[0]*27 for i in range(D+1)]
    for i in range(1,D+1):
        for j in range(i,D+1):
            last[j][T[i]]=i
    sat=0
    for i in range(1,D+1):
        sat+=S[i-1][T[i]-1]
        for j in range(1,27):
            sat-=C[j-1]*(i-last[i][j])
        print(sat)
    
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)]
last = [0 for i in range(26)]
satis = 0
for i in range(1,d+1):
  t=int(input())
  last[t-1]= i
  satis += s[i-1][t-1]
  for j in range(26):
    satis -= c[j] * (i - last[j])
  print(satis)

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

last=[0]*26
score=0
for i in range(D):
    contest=T[i]-1
    score+=S[i][contest]
    last[contest]=i+1
    for j in range(26):
        blank=i+1-last[j]
        score-=blank*C[j]
    print(score)