N,K = gets.chomp.split.map(&:to_i)
P = gets.chomp.split.map(&:to_i)
C = gets.chomp.split.map(&:to_i)

abc = []

(0..N-1).each do |i|
  cnt = 0
  ans = [0]
  y = i+1
while (cnt < K) do
  y = P[y-1]
  ans << ans.last+C[y-1]
  cnt += 1
  break if y == i+1
end
a = ans[1..-1].max
if cnt == K
abc << a
else
abc << [((ans.last)*(K/cnt)+ ans[0..K%cnt].max),((ans.last)*(K/cnt-1)+ ans[0..-2].max,a].max 
end
end

puts abc.max



  
  


