n, k = map(int, input().split())
ps = list(map(int, input().split()))
cs = list(map(int, input().split()))
ps = [p-1 for p in ps]
# doubling使う
vers = []   # vers[b][i] 頂点iから2^b回移動した時にどの頂点にいるか
score = []  # score[b][i]頂点iから2^b回移動した時に何点もらえるか
vers.append(ps)
score.append(cs)

m = 31
# ダブリング
for b in range(1, m+1):
    p_bth = [0] * n
    c_bth = [0] * n
    for i in range(n):
        p_bth[i] = vers[b-1][vers[b-1][i]]
        c_bth[i] = score[b-1][i] + score[b-1][vers[b-1][i]]
    vers.append(p_bth)
    score.append(c_bth)

# 桁DP
MIN = -(1 << 63) 
prv = [[MIN, 0] for _ in range(n)]
nxt = [[MIN, MIN] for _ in range(n)] 
for b in range(m, -1, -1):
    for i in range(n):
        if (k >> b) & 1:
            nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i])
            nxt[vers[b][i]][1] = max(nxt[vers[b][i]][1], prv[i][1] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
        else:
            nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0])
            nxt[i][1] = max(nxt[i][1], prv[i][1])
    prv, nxt = nxt, prv

ans = max(max(x) for x in prv)
if ans == 0:
    ans = max(cs)
print(ans) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k ps = list(map(int, input().split())) ps list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split cs = list(map(int, input().split())) cs list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split p-1 for p in ps for p in ps p ps for p in ps p-1 p 1 ps = [p-1 for p in ps] ps [p-1 for p in ps] vers = [] vers [] score = [] score [] vers.append(ps) vers.append vers append ps score.append(cs) score.append score append cs m = 31 m 31 for b in range(1, m+1):
    p_bth = [0] * n
    c_bth = [0] * n
    for i in range(n):
        p_bth[i] = vers[b-1][vers[b-1][i]]
        c_bth[i] = score[b-1][i] + score[b-1][vers[b-1][i]]
    vers.append(p_bth)
    score.append(c_bth)

# 桁DP b range(1, m+1) range 1 m+1 m 1 p_bth = [0] * n p_bth [0] * n [0] 0 n c_bth = [0] * n c_bth [0] * n [0] 0 n for i in range(n):
        p_bth[i] = vers[b-1][vers[b-1][i]]
        c_bth[i] = score[b-1][i] + score[b-1][vers[b-1][i]]
     i range(n) range n p_bth[i] = vers[b-1][vers[b-1][i]] p_bth[i] p_bth i vers[b-1][vers[b-1][i]] [b-1] vers b-1 b 1 vers[b-1][i] [b-1] vers b-1 b 1 i c_bth[i] = score[b-1][i] + score[b-1][vers[b-1][i]] c_bth[i] c_bth i score[b-1][i] + score[b-1][vers[b-1][i]] score[b-1][i] [b-1] score b-1 b 1 i score[b-1][vers[b-1][i]] [b-1] score b-1 b 1 vers[b-1][i] [b-1] vers b-1 b 1 i vers.append(p_bth) vers.append vers append p_bth score.append(c_bth) score.append score append c_bth MIN = -(1 << 63) MIN -(1 << 63) 1 << 63 1 63 [MIN, 0] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [MIN, 0] MIN 0 prv = [[MIN, 0] for _ in range(n)] prv [[MIN, 0] for _ in range(n)] [MIN, MIN] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [MIN, MIN] MIN MIN nxt = [[MIN, MIN] for _ in range(n)] nxt [[MIN, MIN] for _ in range(n)] for b in range(m, -1, -1):
    for i in range(n):
        if (k >> b) & 1:
            nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i])
            nxt[vers[b][i]][1] = max(nxt[vers[b][i]][1], prv[i][1] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
        else:
            nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0])
            nxt[i][1] = max(nxt[i][1], prv[i][1])
    prv, nxt = nxt, prv b range(m, -1, -1) range m -1 -1 for i in range(n):
        if (k >> b) & 1:
            nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i])
            nxt[vers[b][i]][1] = max(nxt[vers[b][i]][1], prv[i][1] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
        else:
            nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0])
            nxt[i][1] = max(nxt[i][1], prv[i][1])
     i range(n) range n if (k >> b) & 1:
            nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i])
            nxt[vers[b][i]][1] = max(nxt[vers[b][i]][1], prv[i][1] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
        else:
            nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0])
            nxt[i][1] = max(nxt[i][1], prv[i][1])
     (k >> b) & 1 k >> b k b 1 nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i]) nxt[vers[b][i]][0] [vers[b][i]] nxt vers[b][i] [b] vers b i 0 max(nxt[vers[b][i]][0], prv[i][0] + score[b][i]) max nxt[vers[b][i]][0] [vers[b][i]] nxt vers[b][i] [b] vers b i 0 prv[i][0] + score[b][i] prv[i][0] [i] prv i 0 score[b][i] [b] score b i nxt[vers[b][i]][1] = max(nxt[vers[b][i]][1], prv[i][1] + score[b][i]) nxt[vers[b][i]][1] [vers[b][i]] nxt vers[b][i] [b] vers b i 1 max(nxt[vers[b][i]][1], prv[i][1] + score[b][i]) max nxt[vers[b][i]][1] [vers[b][i]] nxt vers[b][i] [b] vers b i 1 prv[i][1] + score[b][i] prv[i][1] [i] prv i 1 score[b][i] [b] score b i nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1]) nxt[i][0] [i] nxt i 0 max(nxt[i][0], prv[i][0], prv[i][1]) max nxt[i][0] [i] nxt i 0 prv[i][0] [i] prv i 0 prv[i][1] [i] prv i 1 nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i]) nxt[vers[b][i]][0] [vers[b][i]] nxt vers[b][i] [b] vers b i 0 max(nxt[vers[b][i]][0], prv[i][0] + score[b][i]) max nxt[vers[b][i]][0] [vers[b][i]] nxt vers[b][i] [b] vers b i 0 prv[i][0] + score[b][i] prv[i][0] [i] prv i 0 score[b][i] [b] score b i nxt[i][0] = max(nxt[i][0], prv[i][0]) nxt[i][0] [i] nxt i 0 max(nxt[i][0], prv[i][0]) max nxt[i][0] [i] nxt i 0 prv[i][0] [i] prv i 0 nxt[i][1] = max(nxt[i][1], prv[i][1]) nxt[i][1] [i] nxt i 1 max(nxt[i][1], prv[i][1]) max nxt[i][1] [i] nxt i 1 prv[i][1] [i] prv i 1 prv, nxt = nxt, prv prv nxt nxt prv ans = max(max(x) for x in prv) ans max(max(x) for x in prv) max max(x) max x if ans == 0:
    ans = max(cs) ans == 0 ans 0 ans = max(cs) ans max(cs) max cs print(ans) print ans vers = [] [] vers p_bth = [0] * n [0] * n p_bth m = 31 31 m cs = list(map(int, input().split())) list(map(int, input().split())) cs nxt = [[MIN, MIN] for _ in range(n)] [[MIN, MIN] for _ in range(n)] nxt ans = max(cs) max(cs) ans MIN = -(1 << 63) -(1 << 63) MIN k = map(int, input().split()) map(int, input().split()) k ps = list(map(int, input().split())) list(map(int, input().split())) ps ps = [p-1 for p in ps] [p-1 for p in ps] ps prv = [[MIN, 0] for _ in range(n)] [[MIN, 0] for _ in range(n)] prv score = [] [] score prv, nxt = nxt, prv nxt prv n, k = map(int, input().split()) map(int, input().split()) n c_bth = [0] * n [0] * n c_bth nxt = nxt, prv prv nxt ans = max(max(x) for x in prv) max(max(x) for x in prv) ans
n, k = map(int, input().split())
ps = list(map(int, input().split()))
cs = list(map(int, input().split()))
ps = [p-1 for p in ps]
# doubling使う
vers = []   # vers[b][i] 頂点iから2^b回移動した時にどの頂点にいるか
score = []  # score[b][i]頂点iから2^b回移動した時に何点もらえるか
vers.append(ps)
score.append(cs)

m = 31
# ダブリング
for b in range(1, m+1):
    p_bth = [0] * n
    c_bth = [0] * n
    for i in range(n):
        p_bth[i] = vers[b-1][vers[b-1][i]]
        c_bth[i] = score[b-1][i] + score[b-1][vers[b-1][i]]
    vers.append(p_bth)
    score.append(c_bth)

# 桁DP
MIN = -(1 << 63) 
prv = [[MIN, 0] for _ in range(n)]
nxt = [[MIN, MIN] for _ in range(n)] 
for b in range(m, -1, -1):
    for i in range(n):
        if (k >> b) & 1:
            nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i])
            nxt[vers[b][i]][1] = max(nxt[vers[b][i]][1], prv[i][1] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
        else:
            nxt[vers[b][i]][0] = max(nxt[vers[b][i]][0], prv[i][0] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0])
            nxt[i][1] = max(nxt[i][1], prv[i][1])
    prv, nxt = nxt, prv

ans = max(max(x) for x in prv)
if ans == 0:
    ans = max(cs)
print(ans)