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:
D = int(input())
C = list(map(int,input().split())) #C[i]AiCを実施しないと下がる満足度
S = [] #S[i][j] i日目にAjcというコンテストを実施した場合に上がる満足度
for i in range(D): #コンテストは0index
    temp = list(map(int,input().split()))
    S.append(temp)
T = []
for i in range(D):
    temp = int(input())
    T.append(temp)
since = [0 for _ in range(26)] #最後に開催されてから何日経ったか。
manzoku = 0
for i in range(26):
    since[i] += C[i]
for i in range(D):
    contest = T[i]-1 #0index
    manzoku += S[i][contest]
    for j in range(26):
        if j != contest:
            manzoku -= since[j]
    print(manzoku)
    for j in range(26):
        if j != contest:
            since[j] += C[j]
        else:
            since[j] = C[j]

Submission status: Accepted

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

last = [0] * 26

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

Submission status: Accepted

Solution code:
d=int(input())

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

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

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

        
    ans.append(score)


for item in ans:
    print(item)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
import numpy as np

def scores(c, s, t):
    last = [0] * 26
    V = []
    lastv = 0
    for d, i in enumerate(t):
        last[i] = d+1
        v = s[d*26+i]
        sub = sum([c[j]*(d+1-last[j]) for j in range(26)])
        # print(v, sub)
        lastv += v-sub
        V.append(lastv)
    return V


def main():
    D, *cs = map(int, read().split())
    c = cs[:26]
    s = cs[26:-D]
    t = [x-1 for x in cs[-D:]]

    v = scores(c, s, t)
    print(*v, sep='\n')
    # c = cs[:26]
    # s = cs[26:]
    # print(D, c)
    # print(s)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

# B
T = [int(input()) for _ in range(D)]

# 初期化
ans = 0 # 満足度
last = [-1]*26

# 満足度を計算
for d in range(D):
    # 開催するコンテストID
    contest_id = T[d]-1
    # 満足度をプラス
    ans += S[d][contest_id]
    # 最終開催日を更新
    last[contest_id] = d
    # 開催されなかったコンテストの処理
    for i in range(26):
        ans -= C[i]*(d - last[i])
    # 出力
    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)]
t = [int(input()) - 1 for _ in range(d)]

def scoring(d, c, s, t):
    v = []
    memo = [0 for _ in range(26)]
    for i in range(d):
        if i == 0:
            score = 0
        else:
            score = v[-1]
        score += s[i][t[i]]
        memo[t[i]] = i + 1
        for j in range(26):
            score -= c[j] * (i+1 - memo[j])

        v.append(score)

    return v

v = scoring(d, c, s, t)
print(*v, sep='\n')

Submission status: Accepted

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

L = [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]
sum = 0
for i in range(D):
    t = int(input())
    s = S[i]
    sum += s[t - 1]
    for j in range(26):
        if j == t - 1:
            L[t - 1] = i + 1
        else:
            sum -= C[j] * (i + 1 - L[j])
    print(sum)

Submission status: Accepted

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**9)
    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())-1 for _ in range(D) ]
    # 最後に開かれた日付
    last = [0]*26

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

    score = 0
    for i in range(D):
        score += S[i][T[i]]
        last[T[i]] = i+1
        c = cp(i+1, T[i])
        score -= c
        print(score)


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)]
T = [int(input()) for _ in range(D)]

ans = []
val = 0
sch = [0]*26
for d in range(D):
    day = d+1
    t = T[d]-1
    sch[t] = day

    down = 0
    for i in range(26):
        down += C[i]*(day - sch[i])

    val = val + S[d][t] - down
    ans.append(val)

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# モジュールのインポート
import numpy as np

# 標準入力を取得
D = int(input())
c = list(map(int, input().split()))
s = []
for d in range(D):
    s_d = list(map(int, input().split()))
    s.append(s_d)
t = []
for d in range(D):
    t_d = int(input())
    t.append(t_d)

c = np.array(c)
s = np.array(s)
t = np.array(t)

# 求解処理
KIND = 26
last = np.array([0 for i in range(KIND)])
S = 0
for d in range(1, D + 1):
    S += s[d - 1][t[d - 1] - 1]
    last[t[d - 1] - 1] = d
    S -= sum(c * (d - last))
    print(S)

Submission status: Accepted

Solution code:
d = int(input())
c = [0] + list(map(int, input().split()))

s = [0]
for i in range(d):
  s.append([0] + list(map(int, input().split())))
  
t = [0]
for i in range(d):
  t.append(int(input()))
  
sati = [0] * (d+1)
last = [0] * 27

for i in range(1, d+1):
  sati[i] = sati[i-1] + s[i][t[i]]
  unsati = 0
  last[t[i]] = i
  for j in range(1, 27):
    unsati += c[j] * (i - last[j])
  sati[i] -= unsati
  
for i in range(1, d+1):
  print(sati[i])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
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)]

M = 26
last = [0]*M
score = 0
for i in range(D):
    score += S[i][t[i]-1]
    last[t[i]-1] = i+1
    for m in range(M):
        score -= C[m]*(i+1-last[m])
    print(score)

Submission status: Accepted

Solution code:
D = int(input())
c = [int(i) for i in input().split()]
s = []
for i in range(D):
  tmp = [int(j) for j in input().split()]
  s.append(tmp)
t = []
for i in range(D):
  t.append(int(input()))
  
sat = 0
lst = [0 for i in range(26)]

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

Submission status: Accepted

Solution code:
D = int(input())
Cs = list(map(int,input().split()))
Ss = [list(map(int,input().split())) for i in range(D)]
ts = []
for i in range(D):
    ts.append(int(input()))

last = [0]*26
satisfy = 0

for day,t in enumerate(ts):
    day += 1
    last[t-1] = day
    satisfy += Ss[day-1][t-1]
    for i in range(26):
        satisfy -= Cs[i]*(day-last[i])
    print(satisfy)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
def solve(LIST):
    ans=0
    last=[0]*26
    for i in range(len(LIST)):
        a=i+1 #day a0
        x=LIST[i]-1
        #increase        
        ans+=S[i][x]
        #decrease
        last[x]=a#update
        temp=0
        for j in range(26):
            temp+=C[j]*(a-last[j])
        #print(ans,temp,"deb")
        ans-=temp
        print(ans)
    
    return  
def resolve():
    global D,C,S
    #input of prob.A
    D,=pin()
    C=[*pin()]#0-indexed
    S=[[*pin()]for i in range(D)]

    #output of prob.A
    T=[int(input()) for i in range(D)]#0indexed
    solve(T)

    
#%%submit!
resolve()

Submission status: Accepted

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

ans=[0]*(d+1)
v=sum(c)

# 最後にどの試験がいつ行われたか？
PL=[0]*26

for i in range(d):
    x=t[i]-1
    ans[i+1]+=ans[i]+D[i][x]
    PL[x]=i+1
    for j in range(26):
        ans[i+1]-=(i+1-PL[j])*c[j]

for i in range(1,d+1):
    print(ans[i])

Submission status: Accepted

Solution code:
d = int(input())
cs = list(map(int, input().split()))
sm = [list(map(int, input().split())) for _ in range(d)]
ts = [int(input()) for _ in range(d)]
ls = [0] * 26

s = 0
for i in range(d):
    t = ts[i]
    t -= 1
    s += sm[i][t]
    ls[t] = i + 1
    #print('contest',t+1)
    #print('plus',sm[i][t])
    #print(ls)
    #print(cs)

    dis = sum(c * ((i+1) - l) for c, l in zip(cs, ls))
    #print('dis',dis)
    s -= dis

    print(s)

Submission status: Accepted

Solution code:
D = int(input())
C = [int(i) for i in input().split()]
scores = [[int(i) for i in input().split()] for j in range(D)]
t = [int(input()) for i in range(D)]

ans = []
S = 0
last = [0]*26
for d in range(1,D+1):
    S += scores[d-1][t[d-1]-1]
    last[t[d-1]-1] = d
    for i in range(26):
        S -= C[i] * (d - last[i])
    ans.append(S)
print("\n".join(map(str, 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)]
T = [int(input()) for _ in range(D)]
v = 0
last = [0] * 26
for d in range(D):
    # ct:current type
    ct= T[d] - 1
    last[ct] = d + 1
    # 今回の満足度
    v += S[d][ct]
    # 不満度を引く
    v -= sum([C[t] * (d + 1 - last[t]) for t in range(26)])
    print(v)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    D = int(input())
    c = list(map(int, input().split()))
    s = []
    for _ in range(D):
        si = list(map(int, input().split()))
        s.append(si)
    score = 0
    last_list = [0] * 26
    for i in range(D):
        t = int(input())
        last_list[t-1] = i+1
        current = s[i][t-1]
        minus = 0
        for j in range(26):
            if t - 1 != j:
                minus += c[j] * (i+1-last_list[j])
        current -= minus
        score += current
        print(score)
if __name__ == "__main__":
    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)]
A = [0 for i in range(26)]
ans = 0
for i in range(D):
  t = int(input())
  A[t-1] = i+1
  c = 0
  for j in range(26):
    c += C[j] * (i+1-A[j])
  ans += S[i][t-1]-c
  print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from collections import defaultdict
from heapq import heappush, heappop
import sys
import numpy as np


sys.setrecursionlimit(10**6)
input = sys.stdin.buffer.readline
INF = 10 ** 9 + 1  # sys.maxsize # float("inf")


def debug(*x):
    print(*x)


def solve():
    "void()"
    pass


def main():
    D = int(input())
    CS = list(map(int, input().split()))
    data = np.int16(read().split())
    S = data[:D * 26].reshape((D, 26))
    answer = data[D * 26:]

    last = [-1] * 26
    score = 0
    for i in range(D):
        j = answer[i] - 1
        score += S[i, j]
        last[j] = i
        score -= sum(CS[j] * (i - last[j]) for j in range(26))
        print(score)
    # solve()


T = """
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
"""

OUT = """
1
17
13
14
13
"""


def _test():
    """
    >>> as_input(T)
    >>> main()
    18398
    35037
    51140
    65837
    79325
    """
    import doctest
    doctest.testmod()


def as_input(s):
    "use in test, use given string as input file"
    import io
    global read, input
    f = io.StringIO(s.strip())
    input = f.readline
    read = f.read


USE_NUMBA = False
if (USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE') or sys.argv[-1] == '-c':
    print("compiling")
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('solve', solve.__doc__.strip().split()[0])(solve)
    cc.compile()
    exit()
else:
    input = sys.stdin.buffer.readline
    read = sys.stdin.buffer.read

    if (USE_NUMBA and sys.argv[-1] != '-p') or sys.argv[-1] == "--numba":
        # -p: pure python mode
        # if not -p, import compiled module
        from my_module import solve  # pylint: disable=all
    elif sys.argv[-1] == "-t":
        _test()
        sys.exit()
    elif sys.argv[-1] != '-p' and len(sys.argv) == 2:
        # input given as file
        input_as_file = open(sys.argv[1])
        input = input_as_file.buffer.readline
        read = input_as_file.buffer.read

    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)]
t = [int(input()) for _ in range(D)]

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union
# import time
# import math
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import Fraction               # Fraction(a, b) => a / b ∈ Q. note: Fraction(0.1) do not returns Fraciton(1, 10). Fraction('0.1') returns Fraction(1, 10)



def main():
    Num = Union[int, float]
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79e+308
    # inf = 2 ** 63 - 1             # (for fast JIT compile in PyPy) 9.22e+18
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def isp():   return input().split()
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    def calc_total_score(schedule):
        ans = []
        last = [-1] * 26
        satisfy = 0
        for day in range(D):
            contest_id = schedule[day]
            last[contest_id] = day
            satisfy += score[day][contest_id]
            for i in range(26):
                satisfy -= c[i] * (day - last[i])
            ans.append(satisfy)
        return ans
    

    D = ii()
    c = lmi()
    score = [lmi() for _ in range(D)]

    schedule = [ii() - 1 for _ in range(D)]
    print(*calc_total_score(schedule), sep='\n')




if __name__ == "__main__":
    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)]

last=[0]*26
ans=0

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

Submission status: Accepted

Solution code:
def main():
    d = int(input())
    c = [int(i) for i in input().split()]
    s = [[int(i) for i in input().split()] for _ in range(d)]
    t = [int(input()) - 1 for _ in range(d)]
    score = 0
    last = [0] * 26
    for i in range(d):
        score += s[i][t[i]]
        last[t[i]] = i + 1
        for j in range(26):
            score -= c[j] * (i + 1 - last[j])
        print(score)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
d = int(input())
clist = list(map(int,input().split()))
last_c = [0]*26

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

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

satisfy = [0]*(d+1)

for i in range(1,d+1):
    satisfy[i] = slist[i-1][tlist[i-1]-1] + satisfy[i-1]
    last_c[tlist[i-1]-1] = i
    for j in range(26):
        satisfy[i] -= clist[j] * (i - last_c[j])

#remove first
satisfy.remove(0)

for i in satisfy:
    print(i)

Submission status: Accepted

Solution code:
def main():
    d = int(input())
    c = [int(v) for v in input().split()]
    last = [-1] * 26
    s = []
    for _ in range(d):
        s.append([int(v) for v in input().split()])
    t = []
    for _ in range(d):
        t.append(int(input()))

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

main()

Submission status: Accepted

Solution code:
D = int(input())
c = list(map(int, input().split()))
s = list()
for _ in range(D):
	s.append(list(map(int, input().split())))
t = list()
for _ in range(D):
	t.append(int(input()))
v = 0
l = [0 for _ in range(26)]
def f(date, l):
	tmp = 0
	for i in range(26):
		tmp += c[i]*(date + 1 - l[i])
	return tmp

for i in range(D):
	v += s[i][t[i] - 1]
	l[t[i] - 1] = i + 1
	v -= f(i, l)
	print(v)

Submission status: Accepted

Solution code:
D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for _ in range(D)]
t = [int(input()) for _ in range(D)]
ans = 0
for i in range(D):
    ans += S[i][t[i]-1]
    manzoku_down = 0
    for k in range(26):
        last = 0
        for j in range(0,i+1):
            if t[j] == k+1:
                last = j+1
        manzoku_down += C[k]*((i+1)-last)
    ans -= manzoku_down
    print(ans)

Submission status: Accepted

Solution code:
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 = [-1] * 26

v = 0

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

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

s_rate = 0

last_days = [0] * 26

for day in range(d):
    s_rate += s[day][t[day]-1]
    last_days[t[day]-1] = day + 1
    for j in range(26):
        s_rate -= c[j] * (day + 1 - last_days[j])
    print(s_rate)

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

satisfaction = 0
last = [0]*26

for i in range(D):
    satisfaction += S[i][T[i]-1]
#     コンテスト開催による満足度増加
    last[T[i]-1] = i+1
#     last 更新

    satisfaction -= sum([C[k]*((i+1)-last[k]) for k in range(26)])
#     開催していない分の満足度降下

    print(satisfaction)

Submission status: Accepted

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


def inpl():
    return list(map(int, input().split()))


def count_v(last_d, C, S, t, d):
    ret = S[t - 1]
    last_d[t - 1] = d
    for i in range(len(last_d)):
        ret -= C[i] * (d - last_d[i])
    return ret


def main():
    D = int(input())
    C = inpl()
    Ss = [inpl() for _ in range(D)]
    T = [int(input()) for _ in range(D)]
    last_d = [-1] * len(C)

    v = 0
    for d in range(D):
        tmp = count_v(last_d, C, Ss[d], T[d], d)
        v += tmp
        print(v)

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import bisect
from functools import lru_cache
from collections import defaultdict
inf = float('inf')
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def input(): return sys.stdin.readline().rstrip()
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
d=read()
c=list(reads())
s=[[]for i in range(d) ]
for i in range(d):
    s[i]=list(reads())
t=[[]for i in range(d)]
for i in range(d):
    t[i]=read()
    t[i]-=1
contestcount=26
last=[0 for i in range(contestcount)]
score=0
for i in range(d):
    score+=s[i][t[i]]
    last[t[i]]=i+1
    for j in range(contestcount):
        score-=c[j]*(i+1-last[j])
    print(score)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# N,M = map(int,sys.stdin.readline().split())
# a = tuple(map(int,sys.stdin.readline().split())) # single line with multi param
# a = tuple(int(sys.stdin.readline()) for _ in range(N)) # multi line with single param
# a = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(N)) # multi line with multi param
# s = sys.stdin.readline().rstrip()
# N = int(sys.stdin.readline())
# INF = float("inf")
import sys

D = int(sys.stdin.readline())
c = tuple(map(int,sys.stdin.readline().split())) # single line with multi param
s = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(D)) # multi line with multi param
t = tuple(int(sys.stdin.readline()) for _ in range(D)) # multi line with single param

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