n,k = gets.split.map &:to_i
ps = gets.split.map &:to_i
cs = gets.split.map &:to_i

def max(loop, k)
	window = [loop.size, k].min
	n = Array.new(window, nil)
	
	(1 .. window).each{ |w|
		start = loop[0...w].sum
		max = start
	
		(0 ... (loop.size - w)).each{ |i|
			start = start - loop[i] + loop[i + w]
			if start > max then
				max = start
			end
		}
		
		n[w - 1] = max
	}
	
	if k > loop.size && n[loop.size - 1] > 0 then
		n[loop.size - 1] * (k / loop.size) + n[1 .. k % loop.size - 1].max
	else
		n.max
	end
end

walked = Array.new(ps.size, false)


result = nil

ps.each_with_index{ |p, i|
	if walked[i] == false then
		walked[i] = true

		loop = [p - 1]
		next_p = p - 1
		
		while ! loop.include?(ps[next_p] - 1)
			walked[next_p] = true
			loop << ps[next_p] - 1
			next_p = ps[next_p] - 1
		end
		
		loop_v = []
		
		loop.each{ |ind|
			loop_v << cs[ind]
		}
		
		f = max(loop_v, k)
		if result == nil || f > result then
			result = f
		end
	end
}

puts result
