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

for i, p in enumerate(map(int, input().split())):
    P[i+1] = p
for i, c in enumerate(map(int, input().split())):
    C[i+1] = c

ans = -float("inf")
for pos in range(1, N+1):
    i = pos
    score = 0
    for k in range(1, K+1):
        i = P[i]
        score += C[i]
        ans = max(ans, score)
        if i == pos:
            if score > 0:
                lastAns = score * ((K//k)-1)
                NumberOfCycle = (K%k) + k
                lastScore = lastAns
                j = i
                for _ in range(NumberOfCycle):
                    j = P[j]
                    lastScore += C[j]
                    lastAns = max(lastAns, lastScore)
                ans = max(ans, lastAns)
            break
print(ans) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = dict() P dict() dict C = dict() C dict() dict for i, p in enumerate(map(int, input().split())):
    P[i+1] = p i p enumerate(map(int, input().split())) enumerate map(int, input().split()) map int input().split() ().split () input split P[i+1] = p P[i+1] P i+1 i 1 p for i, c in enumerate(map(int, input().split())):
    C[i+1] = c i c enumerate(map(int, input().split())) enumerate map(int, input().split()) map int input().split() ().split () input split C[i+1] = c C[i+1] C i+1 i 1 c ans = -float("inf") ans -float("inf") float("inf") float "inf" for pos in range(1, N+1):
    i = pos
    score = 0
    for k in range(1, K+1):
        i = P[i]
        score += C[i]
        ans = max(ans, score)
        if i == pos:
            if score > 0:
                lastAns = score * ((K//k)-1)
                NumberOfCycle = (K%k) + k
                lastScore = lastAns
                j = i
                for _ in range(NumberOfCycle):
                    j = P[j]
                    lastScore += C[j]
                    lastAns = max(lastAns, lastScore)
                ans = max(ans, lastAns)
            break pos range(1, N+1) range 1 N+1 N 1 i = pos i pos score = 0 score 0 for k in range(1, K+1):
        i = P[i]
        score += C[i]
        ans = max(ans, score)
        if i == pos:
            if score > 0:
                lastAns = score * ((K//k)-1)
                NumberOfCycle = (K%k) + k
                lastScore = lastAns
                j = i
                for _ in range(NumberOfCycle):
                    j = P[j]
                    lastScore += C[j]
                    lastAns = max(lastAns, lastScore)
                ans = max(ans, lastAns)
            break k range(1, K+1) range 1 K+1 K 1 i = P[i] i P[i] P i score += C[i] score C[i] C i ans = max(ans, score) ans max(ans, score) max ans score if i == pos:
            if score > 0:
                lastAns = score * ((K//k)-1)
                NumberOfCycle = (K%k) + k
                lastScore = lastAns
                j = i
                for _ in range(NumberOfCycle):
                    j = P[j]
                    lastScore += C[j]
                    lastAns = max(lastAns, lastScore)
                ans = max(ans, lastAns)
            break i == pos i pos if score > 0:
                lastAns = score * ((K//k)-1)
                NumberOfCycle = (K%k) + k
                lastScore = lastAns
                j = i
                for _ in range(NumberOfCycle):
                    j = P[j]
                    lastScore += C[j]
                    lastAns = max(lastAns, lastScore)
                ans = max(ans, lastAns)
             score > 0 score 0 lastAns = score * ((K//k)-1) lastAns score * ((K//k)-1) score (K//k)-1 K//k K k 1 NumberOfCycle = (K%k) + k NumberOfCycle (K%k) + k K%k K k k lastScore = lastAns lastScore lastAns j = i j i for _ in range(NumberOfCycle):
                    j = P[j]
                    lastScore += C[j]
                    lastAns = max(lastAns, lastScore)
                 _ range(NumberOfCycle) range NumberOfCycle j = P[j] j P[j] P j lastScore += C[j] lastScore C[j] C j lastAns = max(lastAns, lastScore) lastAns max(lastAns, lastScore) max lastAns lastScore ans = max(ans, lastAns) ans max(ans, lastAns) max ans lastAns break print(ans) print ans ans = max(ans, lastAns) max(ans, lastAns) ans i = P[i] P[i] i lastAns = score * ((K//k)-1) score * ((K//k)-1) lastAns C = dict() dict() C ans = max(ans, score) max(ans, score) ans N, K = map(int, input().split()) map(int, input().split()) N K = map(int, input().split()) map(int, input().split()) K score = 0 0 score NumberOfCycle = (K%k) + k (K%k) + k NumberOfCycle lastScore = lastAns lastAns lastScore P = dict() dict() P ans = -float("inf") -float("inf") ans i = pos pos i j = P[j] P[j] j score += C[i] C[i] score lastAns = max(lastAns, lastScore) max(lastAns, lastScore) lastAns lastScore += C[j] C[j] lastScore j = i i j
N, K = map(int, input().split())
P = dict()
C = dict()

for i, p in enumerate(map(int, input().split())):
    P[i+1] = p
for i, c in enumerate(map(int, input().split())):
    C[i+1] = c

ans = -float("inf")
for pos in range(1, N+1):
    i = pos
    score = 0
    for k in range(1, K+1):
        i = P[i]
        score += C[i]
        ans = max(ans, score)
        if i == pos:
            if score > 0:
                lastAns = score * ((K//k)-1)
                NumberOfCycle = (K%k) + k
                lastScore = lastAns
                j = i
                for _ in range(NumberOfCycle):
                    j = P[j]
                    lastScore += C[j]
                    lastAns = max(lastAns, lastScore)
                ans = max(ans, lastAns)
            break
print(ans)
