import sys



sys.setrecursionlimit(10 ** 8)



input = sys.stdin.readline





def main():

    N, K = [int(x) for x in input().split()]

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

    C = [int(x) for x in input().split()]



    ans = max(C)

    for i in range(N):

        next = P[i]

        cnt = 0

        tmp = 0



        while True:

            tmp += C[next]

            ans = max(ans, tmp)

            cnt += 1

            if cnt == K or next == i:

                break

            next = P[next]



        if cnt == K or tmp <= 0:

            continue



        q = K // cnt

        r = K % cnt

        next = P[i]

        if q != 0:

            tmp *= (q - 1)

            q -= 1

            r += cnt

            ans = max(ans, tmp)

        else:

            tmp = 0

        while r > 0:

            tmp += C[next]

            ans = max(ans, tmp)

            next = P[next]

            r -= 1

    print(ans)





if __name__ == '__main__':

    main()

import sys



sys.setrecursionlimit(10 ** 8)



input = sys.stdin.readline





def main():

    N, K = [int(x) for x in input().split()]

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

    C = [int(x) for x in input().split()]



    ans = max(C)

    for i in range(N):

        next = P[i]

        cnt = 0

        cycle_value = 0



        while True:

            cycle_value += C[next]

            cnt += 1

            if next == i:

                break

            next = P[next]



        j = 0

        tmp = 0

        next = P[i]

        while True:

            tmp += C[next]

            j += 1

            ans = max(ans, tmp + ((K - j) // cnt) * max(0, cycle_value))

            if j == K or next == i:

                break

            next = P[next]

    print(ans)





if __name__ == '__main__':

    main()

import sys

sys.setrecursionlimit(10**8)
input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]
    ans = max(C)
    for i in range(N):
        next = P[i]
        cnt = 0
        tmp = 0
        while True:
            tmp += C[next]
            ans = max(ans, tmp)
            cnt += 1
            if cnt == K or next == i:
                break
            next = P[next]
        if cnt == K or tmp <= 0:
            continue
        q = K // cnt
        r = K % cnt
        next = P[i]
        if q != 0:
            tmp *= q - 1
            q -= 1
            r += cnt
            ans = max(ans, tmp)
        else:
            tmp = 0
        while r > 0:
            tmp += C[next]
            ans = max(ans, tmp)
            next = P[next]
            r -= 1
    print(ans)


if __name__ == "__main__":
    main()

import sys

sys.setrecursionlimit(10**8)
input = sys.stdin.readline


def main():
    N, K = [int(x) for x in input().split()]
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]
    ans = max(C)
    for i in range(N):
        next = P[i]
        cnt = 0
        cycle_value = 0
        while True:
            cycle_value += C[next]
            cnt += 1
            if next == i:
                break
            next = P[next]
        j = 0
        tmp = 0
        next = P[i]
        while True:
            tmp += C[next]
            j += 1
            ans = max(ans, tmp + ((K - j) // cnt) * max(0, cycle_value))
            if j == K or next == i:
                break
            next = P[next]
    print(ans)


if __name__ == "__main__":
    main()

+        cycle_value = 0 +        while True: +            cycle_value += C[next] +            cnt += 1 +            if next == i: +                break +            next = P[next] +        j = 0 +        next = P[i] -            ans = max(ans, tmp) -            cnt += 1 -            if cnt == K or next == i: +            j += 1 +            ans = max(ans, tmp + ((K - j) // cnt) * max(0, cycle_value)) +            if j == K or next == i: -        if cnt == K or tmp <= 0: -            continue -        q = K // cnt -        r = K % cnt -        next = P[i] -        if q != 0: -            tmp *= q - 1 -            q -= 1 -            r += cnt -            ans = max(ans, tmp) -        else: -            tmp = 0 -        while r > 0: -            tmp += C[next] -            ans = max(ans, tmp) -            next = P[next] -            r -= 1