program D175
    implicit none
    integer(16) :: N, K
    integer(16), allocatable, dimension(:) :: P, C
    integer(16) :: i, j, l, ans, max1, max2
    
    read(5,*) N, K
    allocate(P(N), C(N))
    read(5,*) (P(i), i = 1, N)
    read(5,*) (C(i), i = 1, N)


    do i = 1, N
        ans = 0
        l = i
        do j = 1, K
            l = P(l)
            ans = ans + C(l)
            if (j == 1) then
                max2 = ans
            else
                if (max2 < ans) then
                    max2 = ans
                end if
            end if
        end do

        if (i == 1) then
            max1 = max2
        else
            if (max1 < max2) then
                max1 = max2
            end if
        end if

    end do

    write(6,*) max1

end program D175