N, K = gets.split.map(&:to_i)
P = gets.split.map{|i|i.to_i - 1}
C = gets.split.map(&:to_i)
max = -10 ** 10
N.times{|n|
	score = Array.new(N){0}
	#score = [0]
	i = n
	m = -10 ** 10
	j = 0
	loop{
		i = P[i]
		j += 1
		score[j] = score[j - 1] + C[i]
		#score << score[-1] + C[i]
		if i == n
			break
		end
	}
	#warn score.inspect
	s = score[j]
	if s < 0
		s = 0
	end
	(j + 1).times{|i|
		score[i] += (K - i) / j * s
		if i > 0 && m < score[i]
			m = score[i]
		end
	}
	#warn score.inspect
	#warn m
	if max < m
		max = m
	end
}
puts max
