from collections import deque
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
A = -(1e9 + 5)
for i in range(N):
    x = i
    S = deque()
    s = 0
    while True:
        x = P[x] - 1
        S.append(C[x])
        s += C[x]
        if x == i:
            break
    l = len(S)
    a = 0
    for i in range(l):
        a += S[i]
        if i + 1 > K:
            break
        An = a
        if s > 0:
            An += s * ((K - i - 1) // l)
        A = max(A, An)
print(A) from collections import deque 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 A = -(1e9 + 5) A -(1e9 + 5) 1e9 + 5 1e9 5 for i in range(N):
    x = i
    S = deque()
    s = 0
    while True:
        x = P[x] - 1
        S.append(C[x])
        s += C[x]
        if x == i:
            break
    l = len(S)
    a = 0
    for i in range(l):
        a += S[i]
        if i + 1 > K:
            break
        An = a
        if s > 0:
            An += s * ((K - i - 1) // l)
        A = max(A, An) i range(N) range N x = i x i S = deque() S deque() deque s = 0 s 0 while True:
        x = P[x] - 1
        S.append(C[x])
        s += C[x]
        if x == i:
            break
     True x = P[x] - 1 x P[x] - 1 P[x] P x 1 S.append(C[x]) S.append S append C[x] C x s += C[x] s C[x] C x if x == i:
            break
     x == i x i break l = len(S) l len(S) len S a = 0 a 0 for i in range(l):
        a += S[i]
        if i + 1 > K:
            break
        An = a
        if s > 0:
            An += s * ((K - i - 1) // l)
        A = max(A, An) i range(l) range l a += S[i] a S[i] S i if i + 1 > K:
            break
         i + 1 > K i + 1 i 1 K break An = a An a if s > 0:
            An += s * ((K - i - 1) // l)
         s > 0 s 0 An += s * ((K - i - 1) // l) An s * ((K - i - 1) // l) s (K - i - 1) // l K - i - 1 K - i K i 1 l A = max(A, An) A max(A, An) max A An print(A) print A x = i i x s += C[x] C[x] s An += s * ((K - i - 1) // l) s * ((K - i - 1) // l) An l = len(S) len(S) l S = deque() deque() S a += S[i] S[i] a C = list(map(int, input().split())) list(map(int, input().split())) C N, K = map(int, input().split()) map(int, input().split()) N P = list(map(int, input().split())) list(map(int, input().split())) P K = map(int, input().split()) map(int, input().split()) K An = a a An s = 0 0 s A = max(A, An) max(A, An) A A = -(1e9 + 5) -(1e9 + 5) A a = 0 0 a x = P[x] - 1 P[x] - 1 x
from collections import deque
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
A = -(1e9 + 5)
for i in range(N):
    x = i
    S = deque()
    s = 0
    while True:
        x = P[x] - 1
        S.append(C[x])
        s += C[x]
        if x == i:
            break
    l = len(S)
    a = 0
    for i in range(l):
        a += S[i]
        if i + 1 > K:
            break
        An = a
        if s > 0:
            An += s * ((K - i - 1) // l)
        A = max(A, An)
print(A)