N, K = map(int, input().split())
P = [0] + list(map(int, input().split()))
C = [0] + list(map(int, input().split()))

ans = -10 ** 9
for i in range(1, N + 1):
    start = i
    next_ = start
    loop_score = 0
    loop_count = 0
    while True:
        loop_count += 1
        loop_score += C[next_]
        next_ = P[next_]
        if next_ == start:
            break
    in_score = 0
    in_count = 0

    for k in range(K):
        in_count += 1
        in_score += C[next_]

        loop_num = (K - in_count) // loop_count
        score = in_score + max(0, loop_score) * loop_num
        ans = max(ans, score)

        next_ = P[next_]
        if next_ == start:
            break

print(ans) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = [0] + list(map(int, input().split())) P [0] + list(map(int, input().split())) [0] 0 list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split C = [0] + list(map(int, input().split())) C [0] + list(map(int, input().split())) [0] 0 list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = -10 ** 9 ans -10 ** 9 -10 9 for i in range(1, N + 1):
    start = i
    next_ = start
    loop_score = 0
    loop_count = 0
    while True:
        loop_count += 1
        loop_score += C[next_]
        next_ = P[next_]
        if next_ == start:
            break
    in_score = 0
    in_count = 0

    for k in range(K):
        in_count += 1
        in_score += C[next_]

        loop_num = (K - in_count) // loop_count
        score = in_score + max(0, loop_score) * loop_num
        ans = max(ans, score)

        next_ = P[next_]
        if next_ == start:
            break i range(1, N + 1) range 1 N + 1 N 1 start = i start i next_ = start next_ start loop_score = 0 loop_score 0 loop_count = 0 loop_count 0 while True:
        loop_count += 1
        loop_score += C[next_]
        next_ = P[next_]
        if next_ == start:
            break
     True loop_count += 1 loop_count 1 loop_score += C[next_] loop_score C[next_] C next_ next_ = P[next_] next_ P[next_] P next_ if next_ == start:
            break
     next_ == start next_ start break in_score = 0 in_score 0 in_count = 0 in_count 0 for k in range(K):
        in_count += 1
        in_score += C[next_]

        loop_num = (K - in_count) // loop_count
        score = in_score + max(0, loop_score) * loop_num
        ans = max(ans, score)

        next_ = P[next_]
        if next_ == start:
            break k range(K) range K in_count += 1 in_count 1 in_score += C[next_] in_score C[next_] C next_ loop_num = (K - in_count) // loop_count loop_num (K - in_count) // loop_count K - in_count K in_count loop_count score = in_score + max(0, loop_score) * loop_num score in_score + max(0, loop_score) * loop_num in_score max(0, loop_score) * loop_num max(0, loop_score) max 0 loop_score loop_num ans = max(ans, score) ans max(ans, score) max ans score next_ = P[next_] next_ P[next_] P next_ if next_ == start:
            break next_ == start next_ start break print(ans) print ans N, K = map(int, input().split()) map(int, input().split()) N ans = max(ans, score) max(ans, score) ans loop_count += 1 1 loop_count in_score += C[next_] C[next_] in_score loop_score = 0 0 loop_score loop_num = (K - in_count) // loop_count (K - in_count) // loop_count loop_num next_ = start start next_ loop_score += C[next_] C[next_] loop_score K = map(int, input().split()) map(int, input().split()) K loop_count = 0 0 loop_count in_count += 1 1 in_count score = in_score + max(0, loop_score) * loop_num in_score + max(0, loop_score) * loop_num score next_ = P[next_] P[next_] next_ C = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) C in_count = 0 0 in_count ans = -10 ** 9 -10 ** 9 ans in_score = 0 0 in_score next_ = P[next_] P[next_] next_ P = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) P start = i i start
N, K = map(int, input().split())
P = [0] + list(map(int, input().split()))
C = [0] + list(map(int, input().split()))

ans = -10 ** 9
for i in range(1, N + 1):
    start = i
    next_ = start
    loop_score = 0
    loop_count = 0
    while True:
        loop_count += 1
        loop_score += C[next_]
        next_ = P[next_]
        if next_ == start:
            break
    in_score = 0
    in_count = 0

    for k in range(K):
        in_count += 1
        in_score += C[next_]

        loop_num = (K - in_count) // loop_count
        score = in_score + max(0, loop_score) * loop_num
        ans = max(ans, score)

        next_ = P[next_]
        if next_ == start:
            break

print(ans)
