def main():
    INF = 10**9
    n,k = map(int,input().split())
    P = [0] + list(map(int,input().split()))
    C = [0] + list(map(int,input().split()))

    scores = [0]*n

    for s in range(1,n+1):
        start = s
        dp = [-INF]*(n+1)
        dp[0] = 0
        for i in range(n):
            p = P[s]
            dp[i+1] = dp[i] + C[p]
            
            if start == p:
                break
            s = p
        if k <= i+1:
            scores[start-1] = max(dp[1:k+1])
        elif dp[i+1]<=0:
            scores[start-1] = max(dp[1:])
        else:
            r = k%(i+1)
            q = k//(i+1)
            if r>0:
                scores[start-1] = max(max(dp[1:r+1])+dp[i+1]*q, max(dp[1:]))
            else:
                scores[start-1] = dp[i+1]*(q-1)+max(dp[1:])

    print(max(scores))
main() def main():
    INF = 10**9
    n,k = map(int,input().split())
    P = [0] + list(map(int,input().split()))
    C = [0] + list(map(int,input().split()))

    scores = [0]*n

    for s in range(1,n+1):
        start = s
        dp = [-INF]*(n+1)
        dp[0] = 0
        for i in range(n):
            p = P[s]
            dp[i+1] = dp[i] + C[p]
            
            if start == p:
                break
            s = p
        if k <= i+1:
            scores[start-1] = max(dp[1:k+1])
        elif dp[i+1]<=0:
            scores[start-1] = max(dp[1:])
        else:
            r = k%(i+1)
            q = k//(i+1)
            if r>0:
                scores[start-1] = max(max(dp[1:r+1])+dp[i+1]*q, max(dp[1:]))
            else:
                scores[start-1] = dp[i+1]*(q-1)+max(dp[1:])

    print(max(scores)) main INF = 10**9 INF 10**9 10 9 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 scores = [0]*n scores [0]*n [0] 0 n for s in range(1,n+1):
        start = s
        dp = [-INF]*(n+1)
        dp[0] = 0
        for i in range(n):
            p = P[s]
            dp[i+1] = dp[i] + C[p]
            
            if start == p:
                break
            s = p
        if k <= i+1:
            scores[start-1] = max(dp[1:k+1])
        elif dp[i+1]<=0:
            scores[start-1] = max(dp[1:])
        else:
            r = k%(i+1)
            q = k//(i+1)
            if r>0:
                scores[start-1] = max(max(dp[1:r+1])+dp[i+1]*q, max(dp[1:]))
            else:
                scores[start-1] = dp[i+1]*(q-1)+max(dp[1:])

     s range(1,n+1) range 1 n+1 n 1 start = s start s dp = [-INF]*(n+1) dp [-INF]*(n+1) [-INF] -INF INF n+1 n 1 dp[0] = 0 dp[0] dp 0 0 for i in range(n):
            p = P[s]
            dp[i+1] = dp[i] + C[p]
            
            if start == p:
                break
            s = p
         i range(n) range n p = P[s] p P[s] P s dp[i+1] = dp[i] + C[p] dp[i+1] dp i+1 i 1 dp[i] + C[p] dp[i] dp i C[p] C p if start == p:
                break
             start == p start p break s = p s p if k <= i+1:
            scores[start-1] = max(dp[1:k+1])
        elif dp[i+1]<=0:
            scores[start-1] = max(dp[1:])
        else:
            r = k%(i+1)
            q = k//(i+1)
            if r>0:
                scores[start-1] = max(max(dp[1:r+1])+dp[i+1]*q, max(dp[1:]))
            else:
                scores[start-1] = dp[i+1]*(q-1)+max(dp[1:])

     k <= i+1 k i+1 i 1 scores[start-1] = max(dp[1:k+1]) scores[start-1] scores start-1 start 1 max(dp[1:k+1]) max dp[1:k+1] dp 1:k+1 1 k+1 k 1 elif dp[i+1]<=0:
            scores[start-1] = max(dp[1:])
         dp[i+1]<=0 dp[i+1] dp i+1 i 1 0 scores[start-1] = max(dp[1:]) scores[start-1] scores start-1 start 1 max(dp[1:]) max dp[1:] dp 1: 1 r = k%(i+1) r k%(i+1) k i+1 i 1 q = k//(i+1) q k//(i+1) k i+1 i 1 if r>0:
                scores[start-1] = max(max(dp[1:r+1])+dp[i+1]*q, max(dp[1:]))
            else:
                scores[start-1] = dp[i+1]*(q-1)+max(dp[1:])

     r>0 r 0 scores[start-1] = max(max(dp[1:r+1])+dp[i+1]*q, max(dp[1:])) scores[start-1] scores start-1 start 1 max(max(dp[1:r+1])+dp[i+1]*q, max(dp[1:])) max max(dp[1:r+1])+dp[i+1]*q max(dp[1:r+1]) max dp[1:r+1] dp 1:r+1 1 r+1 r 1 dp[i+1]*q dp[i+1] dp i+1 i 1 q max(dp[1:]) max dp[1:] dp 1: 1 scores[start-1] = dp[i+1]*(q-1)+max(dp[1:]) scores[start-1] scores start-1 start 1 dp[i+1]*(q-1)+max(dp[1:]) dp[i+1]*(q-1) dp[i+1] dp i+1 i 1 q-1 q 1 max(dp[1:]) max dp[1:] dp 1: 1 print(max(scores)) print max(scores) max scores main() main def main():
    INF = 10**9
    n,k = map(int,input().split())
    P = [0] + list(map(int,input().split()))
    C = [0] + list(map(int,input().split()))

    scores = [0]*n

    for s in range(1,n+1):
        start = s
        dp = [-INF]*(n+1)
        dp[0] = 0
        for i in range(n):
            p = P[s]
            dp[i+1] = dp[i] + C[p]
            
            if start == p:
                break
            s = p
        if k <= i+1:
            scores[start-1] = max(dp[1:k+1])
        elif dp[i+1]<=0:
            scores[start-1] = max(dp[1:])
        else:
            r = k%(i+1)
            q = k//(i+1)
            if r>0:
                scores[start-1] = max(max(dp[1:r+1])+dp[i+1]*q, max(dp[1:]))
            else:
                scores[start-1] = dp[i+1]*(q-1)+max(dp[1:])

    print(max(scores)) def main():
    INF = 10**9
    n,k = map(int,input().split())
    P = [0] + list(map(int,input().split()))
    C = [0] + list(map(int,input().split()))

    scores = [0]*n

    for s in range(1,n+1):
        start = s
        dp = [-INF]*(n+1)
        dp[0] = 0
        for i in range(n):
            p = P[s]
            dp[i+1] = dp[i] + C[p]
            
            if start == p:
                break
            s = p
        if k <= i+1:
            scores[start-1] = max(dp[1:k+1])
        elif dp[i+1]<=0:
            scores[start-1] = max(dp[1:])
        else:
            r = k%(i+1)
            q = k//(i+1)
            if r>0:
                scores[start-1] = max(max(dp[1:r+1])+dp[i+1]*q, max(dp[1:]))
            else:
                scores[start-1] = dp[i+1]*(q-1)+max(dp[1:])

    print(max(scores)) main
def main():
    INF = 10**9
    n,k = map(int,input().split())
    P = [0] + list(map(int,input().split()))
    C = [0] + list(map(int,input().split()))

    scores = [0]*n

    for s in range(1,n+1):
        start = s
        dp = [-INF]*(n+1)
        dp[0] = 0
        for i in range(n):
            p = P[s]
            dp[i+1] = dp[i] + C[p]
            
            if start == p:
                break
            s = p
        if k <= i+1:
            scores[start-1] = max(dp[1:k+1])
        elif dp[i+1]<=0:
            scores[start-1] = max(dp[1:])
        else:
            r = k%(i+1)
            q = k//(i+1)
            if r>0:
                scores[start-1] = max(max(dp[1:r+1])+dp[i+1]*q, max(dp[1:]))
            else:
                scores[start-1] = dp[i+1]*(q-1)+max(dp[1:])

    print(max(scores))
main()