program moving_piece
  implicit none
  integer :: n, k, p(5000) = 0, i, j, m
  integer(8) :: c(5000) = 0, x = -6e18, w, s
  read(*,*) n, k
  read(*,*) p(1:n)
  read(*,*) c(1:n)
  s = max(sum(c(1:n)), 0_8)
  if (k > n) then
    s = s * (k / n - 1)
    k = k - (k / n - 1) * n
  end if
  do i = 1, n
    m = i
    w = 0
    do j = 1, k
      w = w + c(p(m))
      x = max(x, w + s)
      m = p(m)
    end do
  end do
  write(*,'(i0)') x
end program moving_piece