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

lim = 60
nex = [[0] * N for _ in range(lim)]
val = [[0] * N for _ in range(lim)]
cnd = [[0] * N for _ in range(lim)]

for i in range(N):
    nex[0][i] = P[i] - 1
    val[0][i] = C[i]
    cnd[0][i] = C[i]

for d in range(lim - 1):
    for i in range(N):
        nex[d + 1][i] = nex[d][nex[d][i]]
        val[d + 1][i] = val[d][i] + val[d][nex[d][i]]
        cnd[d + 1][i] = max(cnd[d][i], val[d][i] + cnd[d][nex[d][i]])

res = -10 ** 9
for i in range(N):
    sum_ = 0
    offset = i
    for d in reversed(range(lim)):
        if K & 1 << d:
            res = max(res, sum_ + cnd[d][offset])
            sum_ += val[d][offset]
            offset = nex[d][offset]

print(res) 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 lim = 60 lim 60 [0] * N for _ in range(lim) for _ in range(lim) _ range(lim) range lim for _ in range(lim) [0] * N [0] 0 N nex = [[0] * N for _ in range(lim)] nex [[0] * N for _ in range(lim)] [0] * N for _ in range(lim) for _ in range(lim) _ range(lim) range lim for _ in range(lim) [0] * N [0] 0 N val = [[0] * N for _ in range(lim)] val [[0] * N for _ in range(lim)] [0] * N for _ in range(lim) for _ in range(lim) _ range(lim) range lim for _ in range(lim) [0] * N [0] 0 N cnd = [[0] * N for _ in range(lim)] cnd [[0] * N for _ in range(lim)] for i in range(N):
    nex[0][i] = P[i] - 1
    val[0][i] = C[i]
    cnd[0][i] = C[i] i range(N) range N nex[0][i] = P[i] - 1 nex[0][i] [0] nex 0 i P[i] - 1 P[i] P i 1 val[0][i] = C[i] val[0][i] [0] val 0 i C[i] C i cnd[0][i] = C[i] cnd[0][i] [0] cnd 0 i C[i] C i for d in range(lim - 1):
    for i in range(N):
        nex[d + 1][i] = nex[d][nex[d][i]]
        val[d + 1][i] = val[d][i] + val[d][nex[d][i]]
        cnd[d + 1][i] = max(cnd[d][i], val[d][i] + cnd[d][nex[d][i]]) d range(lim - 1) range lim - 1 lim 1 for i in range(N):
        nex[d + 1][i] = nex[d][nex[d][i]]
        val[d + 1][i] = val[d][i] + val[d][nex[d][i]]
        cnd[d + 1][i] = max(cnd[d][i], val[d][i] + cnd[d][nex[d][i]]) i range(N) range N nex[d + 1][i] = nex[d][nex[d][i]] nex[d + 1][i] [d + 1] nex d + 1 d 1 i nex[d][nex[d][i]] [d] nex d nex[d][i] [d] nex d i val[d + 1][i] = val[d][i] + val[d][nex[d][i]] val[d + 1][i] [d + 1] val d + 1 d 1 i val[d][i] + val[d][nex[d][i]] val[d][i] [d] val d i val[d][nex[d][i]] [d] val d nex[d][i] [d] nex d i cnd[d + 1][i] = max(cnd[d][i], val[d][i] + cnd[d][nex[d][i]]) cnd[d + 1][i] [d + 1] cnd d + 1 d 1 i max(cnd[d][i], val[d][i] + cnd[d][nex[d][i]]) max cnd[d][i] [d] cnd d i val[d][i] + cnd[d][nex[d][i]] val[d][i] [d] val d i cnd[d][nex[d][i]] [d] cnd d nex[d][i] [d] nex d i res = -10 ** 9 res -10 ** 9 -10 9 for i in range(N):
    sum_ = 0
    offset = i
    for d in reversed(range(lim)):
        if K & 1 << d:
            res = max(res, sum_ + cnd[d][offset])
            sum_ += val[d][offset]
            offset = nex[d][offset] i range(N) range N sum_ = 0 sum_ 0 offset = i offset i for d in reversed(range(lim)):
        if K & 1 << d:
            res = max(res, sum_ + cnd[d][offset])
            sum_ += val[d][offset]
            offset = nex[d][offset] d reversed(range(lim)) reversed range(lim) range lim if K & 1 << d:
            res = max(res, sum_ + cnd[d][offset])
            sum_ += val[d][offset]
            offset = nex[d][offset] K & 1 << d K 1 << d 1 d res = max(res, sum_ + cnd[d][offset]) res max(res, sum_ + cnd[d][offset]) max res sum_ + cnd[d][offset] sum_ cnd[d][offset] [d] cnd d offset sum_ += val[d][offset] sum_ val[d][offset] [d] val d offset offset = nex[d][offset] offset nex[d][offset] [d] nex d offset print(res) print res res = -10 ** 9 -10 ** 9 res offset = i i offset P = list(map(int, input().split())) list(map(int, input().split())) P cnd = [[0] * N for _ in range(lim)] [[0] * N for _ in range(lim)] cnd lim = 60 60 lim sum_ = 0 0 sum_ N, K = map(int, input().split()) map(int, input().split()) N res = max(res, sum_ + cnd[d][offset]) max(res, sum_ + cnd[d][offset]) res offset = nex[d][offset] nex[d][offset] offset C = list(map(int, input().split())) list(map(int, input().split())) C val = [[0] * N for _ in range(lim)] [[0] * N for _ in range(lim)] val nex = [[0] * N for _ in range(lim)] [[0] * N for _ in range(lim)] nex K = map(int, input().split()) map(int, input().split()) K sum_ += val[d][offset] val[d][offset] sum_
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

lim = 60
nex = [[0] * N for _ in range(lim)]
val = [[0] * N for _ in range(lim)]
cnd = [[0] * N for _ in range(lim)]

for i in range(N):
    nex[0][i] = P[i] - 1
    val[0][i] = C[i]
    cnd[0][i] = C[i]

for d in range(lim - 1):
    for i in range(N):
        nex[d + 1][i] = nex[d][nex[d][i]]
        val[d + 1][i] = val[d][i] + val[d][nex[d][i]]
        cnd[d + 1][i] = max(cnd[d][i], val[d][i] + cnd[d][nex[d][i]])

res = -10 ** 9
for i in range(N):
    sum_ = 0
    offset = i
    for d in reversed(range(lim)):
        if K & 1 << d:
            res = max(res, sum_ + cnd[d][offset])
            sum_ += val[d][offset]
            offset = nex[d][offset]

print(res)
