n, k = gets.split.map &:to_i
ps = gets.split.map { |s| s.to_i - 1 }
cs = gets.split.map { |s| s.to_i }

Record = Struct.new(:score, :max)

rounds = n.times.map do |start|
	i = start
	score = 0
	max = -10**18
	visited = [false] * n
	round = [Record[score, max]]

	while !visited[i]
		visited[i] = true
		j = ps[i]
		score += cs[j]
		max = [max, score].max
		i = j
		round << Record[score, max]
	end

	round
end

max = -10**18
n.times do |start|
	round = rounds[start]
	count = k / (round.size-1)

	if count == 0
		max = [max, round[k].max].max
	elsif round[-1].score < 0
		max = [max, round[-1].max].max
	else
		score = round[-1].score * (count-1)
		max1 = score + round[-1].max
		max2 = score + round[-1].score + round[k % (round.size-1)].max
		max = [max, max1, max2].max
	end
end

p max