N, K = map(int, input().split())
P = list(map(int, input().split()))
_c = list(map(int, input().split()))

# Cを書き換える
C = [0] * N
for i in range(N):
    P[i] -= 1
    C[i] = _c[P[i]]

m = 31 # bit数

# ダブリング
vertex = list()
score = list()
vertex.append(P)
score.append(C)
for b in range(1, m+1):
    p_bth = [0] * N
    c_bth = [0] * N
    for i in range(N):
        p_bth[i] = vertex[b-1][vertex[b-1][i]]
        c_bth[i] = score[b-1][i] + score[b-1][vertex[b-1][i]]
    vertex.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[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 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 b in range(1, m+1):
    p_bth = [0] * N
    c_bth = [0] * N
    for i in range(N):
        p_bth[i] = vertex[b-1][vertex[b-1][i]]
        c_bth[i] = score[b-1][i] + score[b-1][vertex[b-1][i]]
    vertex.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] = vertex[b-1][vertex[b-1][i]]
        c_bth[i] = score[b-1][i] + score[b-1][vertex[b-1][i]]
     i range(N) range N p_bth[i] = vertex[b-1][vertex[b-1][i]] p_bth[i] p_bth i vertex[b-1][vertex[b-1][i]] [b-1] vertex b-1 b 1 vertex[b-1][i] [b-1] vertex b-1 b 1 i c_bth[i] = score[b-1][i] + score[b-1][vertex[b-1][i]] c_bth[i] c_bth i score[b-1][i] + score[b-1][vertex[b-1][i]] score[b-1][i] [b-1] score b-1 b 1 i score[b-1][vertex[b-1][i]] [b-1] score b-1 b 1 vertex[b-1][i] [b-1] vertex b-1 b 1 i vertex.append(p_bth) vertex.append vertex 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[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 c_bth = [0] * N [0] * N c_bth _c = list(map(int, input().split())) list(map(int, input().split())) _c vertex = list() list() vertex ans = max(max(x) for x in prv) max(max(x) for x in prv) ans score = list() list() score K = map(int, input().split()) map(int, input().split()) K C = [0] * N [0] * N C prv = [[MIN, 0] for _ in range(N)] [[MIN, 0] for _ in range(N)] prv MIN = -(1 << 63) -(1 << 63) MIN nxt = [[MIN, MIN] for _ in range(N)] [[MIN, MIN] for _ in range(N)] nxt m = 31 31 m N, K = map(int, input().split()) map(int, input().split()) N prv, nxt = nxt, prv nxt prv ans = max(C) max(C) ans p_bth = [0] * N [0] * N p_bth nxt = nxt, prv prv nxt P = list(map(int, input().split())) list(map(int, input().split())) P
N, K = map(int, input().split())
P = list(map(int, input().split()))
_c = list(map(int, input().split()))

# Cを書き換える
C = [0] * N
for i in range(N):
    P[i] -= 1
    C[i] = _c[P[i]]

m = 31 # bit数

# ダブリング
vertex = list()
score = list()
vertex.append(P)
score.append(C)
for b in range(1, m+1):
    p_bth = [0] * N
    c_bth = [0] * N
    for i in range(N):
        p_bth[i] = vertex[b-1][vertex[b-1][i]]
        c_bth[i] = score[b-1][i] + score[b-1][vertex[b-1][i]]
    vertex.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[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)