from itertools import accumulate





def main():

    cells, moves = [int(x) for x in input().split()]

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

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



    ans = - 10**10

    for start in range(cells):

        route = []

        route_append = route.append

        position = start

        while True:

            route_append(score_on[position])

            position = jumps_from[position]

            if position == start:  # Looped

                break



        length = len(route)

        total = sum(route)

        accum_tmp = 0

        for i, score in enumerate(route):

            accum_tmp += score

            if i + 1 > moves:

                break

            accum = accum_tmp

            if total > 0:

                accum += total * ((moves - (i + 1)) // length)

            ans = max(ans, accum)



        # if total > 0:

        #     q, mod = divmod(moves, length)

        #     if mod == 0:

        #         candidate = total * q

        #     else:

        #         candidate = max(list(accumulate(route))[:mod]) + total * q

        # else:

        #     candidate = max(list(accumulate(route))[:min(moves, length)])

        # ans = max(ans, candidate)

    return ans





if __name__ == '__main__':

    ans = main()

    print(ans)

def main():

    cells, can_jump = [int(x) for x in input().split()]

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

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



    ans = - 10**10

    for start in range(cells):

        route = []

        position = start

        while True:

            route.append(score_on[position])

            position = jumps_from[position]

            if position == start:  # Looped

                break



        length = len(route)

        total = sum(route)

        accum_tmp = 0

        for jumping, score in enumerate(route):

            accum_tmp += score

            if jumping + 1 > can_jump:

                break

            accum = accum_tmp

            if total > 0:

                accum += total * ((can_jump - (jumping + 1)) // length)

            ans = max(ans, accum)

    return ans





if __name__ == '__main__':

    ans = main()

    print(ans)

from itertools import accumulate


def main():
    cells, moves = [int(x) for x in input().split()]
    jumps_from = [int(x) - 1 for x in input().split()]
    score_on = [int(x) for x in input().split()]
    ans = -(10**10)
    for start in range(cells):
        route = []
        route_append = route.append
        position = start
        while True:
            route_append(score_on[position])
            position = jumps_from[position]
            if position == start:  # Looped
                break
        length = len(route)
        total = sum(route)
        accum_tmp = 0
        for i, score in enumerate(route):
            accum_tmp += score
            if i + 1 > moves:
                break
            accum = accum_tmp
            if total > 0:
                accum += total * ((moves - (i + 1)) // length)
            ans = max(ans, accum)
        # if total > 0:
        #     q, mod = divmod(moves, length)
        #     if mod == 0:
        #         candidate = total * q
        #     else:
        #         candidate = max(list(accumulate(route))[:mod]) + total * q
        # else:
        #     candidate = max(list(accumulate(route))[:min(moves, length)])
        # ans = max(ans, candidate)
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)

def main():
    cells, can_jump = [int(x) for x in input().split()]
    jumps_from = [int(x) - 1 for x in input().split()]
    score_on = [int(x) for x in input().split()]
    ans = -(10**10)
    for start in range(cells):
        route = []
        position = start
        while True:
            route.append(score_on[position])
            position = jumps_from[position]
            if position == start:  # Looped
                break
        length = len(route)
        total = sum(route)
        accum_tmp = 0
        for jumping, score in enumerate(route):
            accum_tmp += score
            if jumping + 1 > can_jump:
                break
            accum = accum_tmp
            if total > 0:
                accum += total * ((can_jump - (jumping + 1)) // length)
            ans = max(ans, accum)
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)

-from itertools import accumulate - - -    cells, moves = [int(x) for x in input().split()] +    cells, can_jump = [int(x) for x in input().split()] -        route_append = route.append -            route_append(score_on[position]) +            route.append(score_on[position]) -        for i, score in enumerate(route): +        for jumping, score in enumerate(route): -            if i + 1 > moves: +            if jumping + 1 > can_jump: -                accum += total * ((moves - (i + 1)) // length) +                accum += total * ((can_jump - (jumping + 1)) // length) -        # if total > 0: -        #     q, mod = divmod(moves, length) -        #     if mod == 0: -        #         candidate = total * q -        #     else: -        #         candidate = max(list(accumulate(route))[:mod]) + total * q -        # else: -        #     candidate = max(list(accumulate(route))[:min(moves, length)]) -        # ans = max(ans, candidate)