program moving_piece
  implicit none
  integer(8), parameter :: inf = 2e18
  integer :: n, k, p(5000) = 0, d(5000) = 0, i, m, h, f
  integer(8) :: c(5000) = 0, v(5000) = 0, x = -inf, w
  read(*,*) n, k
  read(*,*) p(1:n)
  read(*,*) c(1:n)
  do i = 1, n
    d(1:n) = -1
    v(1:n) = 0
    d(i) = 0
    m = i
    h = 0
    do while (h < k .and. d(p(m)) < 0)
      d(p(m)) = d(m) + 1
      v(p(m)) = v(m) + c(p(m))
      x = max(x, v(p(m)))
      m = p(m)
      h = h + 1
    end do
    if (h == k) cycle
    w = v(m) + c(p(m)) - v(p(m))
    if (w <= 0) cycle
    f = d(p(m))
    h = d(m) + 1 - d(p(m))
    w = w * ((k - f - 1) / h)
    h = mod(k - f - 1, h) + 1
    m = p(m)
    do while (h > 0)
      x = max(x, v(p(m)) + w)
      m = p(m)
      h = h - 1
    end do
  end do
  write(*,'(i0)') x
end program moving_piece