program D175
    implicit none
    integer(8) :: N, K
    integer(8), allocatable, dimension(:) :: P, C
    integer(8) :: i, j, l, ans
    integer(8), allocatable, dimension(:) :: sc1, sc2
    
    read(5,*) N, K
    allocate(P(N), C(N), sc1(N), sc2(K))
    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)
            sc2(j) = ans
        end do

        sc1(i) = maxval(sc2)
    end do

    write(6,*) maxval(sc1)



end program D175