program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,k,ans,si,i
    integer(int64), allocatable:: p(:), c(:)
        

    read*, n,k
    allocate(p(n), c(n))

    read*, p(:)
    read*, c(:)
    ans = -6e18
    do si=1,n
        block; integer(int64):: x, s(n), l, tot, now,t,e
            x = si
            l=0
            tot = 0
            do while(.true.)
                l=l+1
                x = p(x)
                s(l) = c(x)
                tot = tot + c(x)
                if (x == si) exit
            end do
            t=0
            do i=1,l
                t = t + s(i)
                if (i > k) exit
                now = t
                if (tot > 0)then
                    e = (k-i)/l
                    now = now + tot*e
                end if
                ans = max(ans, now)
            end do
        end block
    end do
    print'(i0)', ans
end program main