n,k = gets.chomp.split(' ').map(&:to_i)
ps = gets.chomp.split(' ').map(&:to_i)
cs = gets.chomp.split(' ').map(&:to_i)
ans = -Float::INFINITY
 
n.times do |i|
    now = i
    score = []
    total = 0
    while true
        now = ps[now] - 1
        score << cs[now]
        total += cs[now]
        if now == i
            break
        end
    end
    t = 0
    score.size.times do |i|
        t += score[i]
        sum = t
        if(i+1>k)
            break
        end
        if(total > 0)
            e = (k-(i+1))/(score.size)
            sum += e*total
        end
        ans = [ans,sum].max
    end

end
puts ans