n, k = list(map(int, input().split()))

P = tuple([int(x)-1 for x in input().split()])

C = tuple(map(int, input().split()))

CC = [[] for _ in range(n)]

for idx, p in enumerate(P):

    CC[idx].append(C[p])

    pp = P[p]

    for _ in range(n):

        CC[idx].append(C[pp])

        pp = P[pp]

        if pp == p:

            break

        

from itertools import accumulate

ans = -2*(10**9)

for cc in CC:

    cca = list(accumulate(cc))

    if cca[-1] <= 0:

        # 1周分のうちの最大値

        if k < len(cc):

            ans = max(ans, max(cca[:k]))

        else:

            ans = max(ans, max(cca))

    else:

        # kk周したほうが良い

        kk, r = divmod(k, len(cc))

        if r == 0:

            ans = max(ans, kk*cca[-1], (kk-1)*cca[-1]+max(cca))

        else:

            ans = max(ans, kk*cca[-1] + max(cca[:r]))

print(ans)

n, k = list(map(int, input().split()))

P = tuple([int(x)-1 for x in input().split()])

C = tuple(map(int, input().split()))

from collections import deque

ans = -2*10**9

for idx, root in enumerate(P):

    scores = [C[root]]

    p = P[root]

    while True:

        scores.append(scores[-1] + C[p])

        p = P[p]

        if p == root:

            break



    sc = scores[-1]

    if sc <= 0:

        if k < len(scores):

            ans = max(ans, max(scores[:k]))

        else:

            ans = max(ans, max(scores))

    else:

        kk, r = divmod(k, len(scores))

        if r == 0:

            ans = max(ans, kk*sc, (kk-1)*sc + max(scores))

        else:

            ans = max(ans, kk*sc + max(scores[:r]))

print(ans)

n, k = list(map(int, input().split()))
P = tuple([int(x) - 1 for x in input().split()])
C = tuple(map(int, input().split()))
CC = [[] for _ in range(n)]
for idx, p in enumerate(P):
    CC[idx].append(C[p])
    pp = P[p]
    for _ in range(n):
        CC[idx].append(C[pp])
        pp = P[pp]
        if pp == p:
            break
from itertools import accumulate

ans = -2 * (10**9)
for cc in CC:
    cca = list(accumulate(cc))
    if cca[-1] <= 0:
        # 1周分のうちの最大値
        if k < len(cc):
            ans = max(ans, max(cca[:k]))
        else:
            ans = max(ans, max(cca))
    else:
        # kk周したほうが良い
        kk, r = divmod(k, len(cc))
        if r == 0:
            ans = max(ans, kk * cca[-1], (kk - 1) * cca[-1] + max(cca))
        else:
            ans = max(ans, kk * cca[-1] + max(cca[:r]))
print(ans)

n, k = list(map(int, input().split()))
P = tuple([int(x) - 1 for x in input().split()])
C = tuple(map(int, input().split()))
from collections import deque

ans = -2 * 10**9
for idx, root in enumerate(P):
    scores = [C[root]]
    p = P[root]
    while True:
        scores.append(scores[-1] + C[p])
        p = P[p]
        if p == root:
            break
    sc = scores[-1]
    if sc <= 0:
        if k < len(scores):
            ans = max(ans, max(scores[:k]))
        else:
            ans = max(ans, max(scores))
    else:
        kk, r = divmod(k, len(scores))
        if r == 0:
            ans = max(ans, kk * sc, (kk - 1) * sc + max(scores))
        else:
            ans = max(ans, kk * sc + max(scores[:r]))
print(ans)

-CC = [[] for _ in range(n)] -for idx, p in enumerate(P): -    CC[idx].append(C[p]) -    pp = P[p] -    for _ in range(n): -        CC[idx].append(C[pp]) -        pp = P[pp] -        if pp == p: +from collections import deque + +ans = -2 * 10**9 +for idx, root in enumerate(P): +    scores = [C[root]] +    p = P[root] +    while True: +        scores.append(scores[-1] + C[p]) +        p = P[p] +        if p == root: -from itertools import accumulate - -ans = -2 * (10**9) -for cc in CC: -    cca = list(accumulate(cc)) -    if cca[-1] <= 0: -        # 1周分のうちの最大値 -        if k < len(cc): -            ans = max(ans, max(cca[:k])) +    sc = scores[-1] +    if sc <= 0: +        if k < len(scores): +            ans = max(ans, max(scores[:k])) -            ans = max(ans, max(cca)) +            ans = max(ans, max(scores)) -        # kk周したほうが良い -        kk, r = divmod(k, len(cc)) +        kk, r = divmod(k, len(scores)) -            ans = max(ans, kk * cca[-1], (kk - 1) * cca[-1] + max(cca)) +            ans = max(ans, kk * sc, (kk - 1) * sc + max(scores)) -            ans = max(ans, kk * cca[-1] + max(cca[:r])) +            ans = max(ans, kk * sc + max(scores[:r]))