n, k = map(int, input().split())
p = list(map(int, input().split()))
_c = list(map(int, input().split()))
c = [0]*n
for i in range(n):
    p[i] -= 1
    c[i] = _c[p[i]]
m = 31
MIN = - (1 << 63)
vertex = list()
score = list()
vertex.append(p)
score.append(c)
for a in range(1, m+1):
    p_ath = [0] * n
    c_ath = [0] * n
    for i in range(n):
        p_ath[i] = vertex[a-1][vertex[a-1][i]]
        c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
    vertex.append(p_ath)
    score.append(c_ath)
    
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[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
            nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
        else:
            nxt[vertex[b][i]][0] = max(nxt[vertex[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(c)
print(ans) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k p = list(map(int, input().split())) p list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split _c = list(map(int, input().split())) _c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c = [0]*n c [0]*n [0] 0 n for i in range(n):
    p[i] -= 1
    c[i] = _c[p[i]] i range(n) range n p[i] -= 1 p[i] p i 1 c[i] = _c[p[i]] c[i] c i _c[p[i]] _c p[i] p i m = 31 m 31 MIN = - (1 << 63) MIN - (1 << 63) 1 << 63 1 63 vertex = list() vertex list() list score = list() score list() list vertex.append(p) vertex.append vertex append p score.append(c) score.append score append c for a in range(1, m+1):
    p_ath = [0] * n
    c_ath = [0] * n
    for i in range(n):
        p_ath[i] = vertex[a-1][vertex[a-1][i]]
        c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
    vertex.append(p_ath)
    score.append(c_ath)
     a range(1, m+1) range 1 m+1 m 1 p_ath = [0] * n p_ath [0] * n [0] 0 n c_ath = [0] * n c_ath [0] * n [0] 0 n for i in range(n):
        p_ath[i] = vertex[a-1][vertex[a-1][i]]
        c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
     i range(n) range n p_ath[i] = vertex[a-1][vertex[a-1][i]] p_ath[i] p_ath i vertex[a-1][vertex[a-1][i]] [a-1] vertex a-1 a 1 vertex[a-1][i] [a-1] vertex a-1 a 1 i c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]] c_ath[i] c_ath i score[a-1][i] + score[a-1][vertex[a-1][i]] score[a-1][i] [a-1] score a-1 a 1 i score[a-1][vertex[a-1][i]] [a-1] score a-1 a 1 vertex[a-1][i] [a-1] vertex a-1 a 1 i vertex.append(p_ath) vertex.append vertex append p_ath score.append(c_ath) score.append score append c_ath [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[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
            nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
        else:
            nxt[vertex[b][i]][0] = max(nxt[vertex[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[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
            nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
        else:
            nxt[vertex[b][i]][0] = max(nxt[vertex[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[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
            nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
        else:
            nxt[vertex[b][i]][0] = max(nxt[vertex[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[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i]) nxt[vertex[b][i]][0] [vertex[b][i]] nxt vertex[b][i] [b] vertex b i 0 max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i]) max nxt[vertex[b][i]][0] [vertex[b][i]] nxt vertex[b][i] [b] vertex b i 0 prv[i][0] + score[b][i] prv[i][0] [i] prv i 0 score[b][i] [b] score b i nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i]) nxt[vertex[b][i]][1] [vertex[b][i]] nxt vertex[b][i] [b] vertex b i 1 max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i]) max nxt[vertex[b][i]][1] [vertex[b][i]] nxt vertex[b][i] [b] vertex 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[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i]) nxt[vertex[b][i]][0] [vertex[b][i]] nxt vertex[b][i] [b] vertex b i 0 max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i]) max nxt[vertex[b][i]][0] [vertex[b][i]] nxt vertex[b][i] [b] vertex 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(c) ans == 0 ans 0 ans = max(c) ans max(c) max c print(ans) print ans prv = [[MIN, 0] for _ in range(n)] [[MIN, 0] for _ in range(n)] prv nxt = nxt, prv prv nxt m = 31 31 m p_ath = [0] * n [0] * n p_ath ans = max(c) max(c) ans _c = list(map(int, input().split())) list(map(int, input().split())) _c c = [0]*n [0]*n c vertex = list() list() vertex prv, nxt = nxt, prv nxt prv score = list() list() score p = list(map(int, input().split())) list(map(int, input().split())) p k = map(int, input().split()) map(int, input().split()) k c_ath = [0] * n [0] * n c_ath n, k = map(int, input().split()) map(int, input().split()) n nxt = [[MIN, MIN] for _ in range(n)] [[MIN, MIN] for _ in range(n)] nxt MIN = - (1 << 63) - (1 << 63) MIN ans = max(max(x) for x in prv) max(max(x) for x in prv) ans
n, k = map(int, input().split())
p = list(map(int, input().split()))
_c = list(map(int, input().split()))
c = [0]*n
for i in range(n):
    p[i] -= 1
    c[i] = _c[p[i]]
m = 31
MIN = - (1 << 63)
vertex = list()
score = list()
vertex.append(p)
score.append(c)
for a in range(1, m+1):
    p_ath = [0] * n
    c_ath = [0] * n
    for i in range(n):
        p_ath[i] = vertex[a-1][vertex[a-1][i]]
        c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
    vertex.append(p_ath)
    score.append(c_ath)
    
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[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
            nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
            nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
        else:
            nxt[vertex[b][i]][0] = max(nxt[vertex[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(c)
print(ans)