from itertools import accumulate

N, K, *PC = map(int, open(0).read().split())
P, C = [0] + PC[:N], [0] + PC[N:]

ans = float("-inf")
for start in range(1, N + 1):
    path = []

    cur = P[start]
    path = [C[start]]
    while cur != start:
        path.append(C[cur])
        cur = P[cur]

    A = list(accumulate(path))
    q, r = divmod(K, len(path))
    res = 0
    if A[-1] >= 0:
        if r == 0:
            q -= 1
            r += len(path)
        res += A[-1] * q
    elif q >= 1:
        r = len(path)

    ans = max(ans, res + max(A[:r]))

print(ans) from itertools import accumulate N, K, *PC = map(int, open(0).read().split()) N map(int, open(0).read().split()) map int open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split K *PC PC P, C = [0] + PC[:N], [0] + PC[N:] P [0] + PC[:N] [0] 0 PC[:N] PC :N N C [0] + PC[N:] [0] 0 PC[N:] PC N: N ans = float("-inf") ans float("-inf") float "-inf" for start in range(1, N + 1):
    path = []

    cur = P[start]
    path = [C[start]]
    while cur != start:
        path.append(C[cur])
        cur = P[cur]

    A = list(accumulate(path))
    q, r = divmod(K, len(path))
    res = 0
    if A[-1] >= 0:
        if r == 0:
            q -= 1
            r += len(path)
        res += A[-1] * q
    elif q >= 1:
        r = len(path)

    ans = max(ans, res + max(A[:r])) start range(1, N + 1) range 1 N + 1 N 1 path = [] path [] cur = P[start] cur P[start] P start path = [C[start]] path [C[start]] C[start] C start while cur != start:
        path.append(C[cur])
        cur = P[cur]

     cur != start cur start path.append(C[cur]) path.append path append C[cur] C cur cur = P[cur] cur P[cur] P cur A = list(accumulate(path)) A list(accumulate(path)) list accumulate(path) accumulate path q, r = divmod(K, len(path)) q divmod(K, len(path)) divmod K len(path) len path r res = 0 res 0 if A[-1] >= 0:
        if r == 0:
            q -= 1
            r += len(path)
        res += A[-1] * q
    elif q >= 1:
        r = len(path)

     A[-1] >= 0 A[-1] A -1 0 if r == 0:
            q -= 1
            r += len(path)
         r == 0 r 0 q -= 1 q 1 r += len(path) r len(path) len path res += A[-1] * q res A[-1] * q A[-1] A -1 q elif q >= 1:
        r = len(path)

     q >= 1 q 1 r = len(path) r len(path) len path ans = max(ans, res + max(A[:r])) ans max(ans, res + max(A[:r])) max ans res + max(A[:r]) res max(A[:r]) max A[:r] A :r r print(ans) print ans res += A[-1] * q A[-1] * q res P, C = [0] + PC[:N], [0] + PC[N:] [0] + PC[:N] P N, K, *PC = map(int, open(0).read().split()) map(int, open(0).read().split()) N path = [C[start]] [C[start]] path r = len(path) len(path) r r += len(path) len(path) r C = [0] + PC[:N], [0] + PC[N:] [0] + PC[N:] C A = list(accumulate(path)) list(accumulate(path)) A r = divmod(K, len(path)) divmod(K, len(path)) r ans = max(ans, res + max(A[:r])) max(ans, res + max(A[:r])) ans K, *PC = map(int, open(0).read().split()) map(int, open(0).read().split()) K cur = P[cur] P[cur] cur cur = P[start] P[start] cur path = [] [] path res = 0 0 res q -= 1 1 q q, r = divmod(K, len(path)) divmod(K, len(path)) q ans = float("-inf") float("-inf") ans
from itertools import accumulate

N, K, *PC = map(int, open(0).read().split())
P, C = [0] + PC[:N], [0] + PC[N:]

ans = float("-inf")
for start in range(1, N + 1):
    path = []

    cur = P[start]
    path = [C[start]]
    while cur != start:
        path.append(C[cur])
        cur = P[cur]

    A = list(accumulate(path))
    q, r = divmod(K, len(path))
    res = 0
    if A[-1] >= 0:
        if r == 0:
            q -= 1
            r += len(path)
        res += A[-1] * q
    elif q >= 1:
        r = len(path)

    ans = max(ans, res + max(A[:r]))

print(ans)