n, k = gets.chomp.split.map(&:to_i)
perm = gets.chomp.split.map(&:to_i)
nums = gets.chomp.split.map(&:to_i)
dp = Array.new(n).map{Array.new(1, 0)}
tales_max = 0
tales_max_idx = 0

n.times do |i|
    idx_p = i
    j = 0
    loop do
        dp[i] << dp[i][j] + nums[idx_p]
        idx_p = perm[idx_p] - 1
        j += 1
        break if idx_p == i
    end
    tales_max = dp[i][-1] if tales_max < dp[i][-1]
    if tales_max < dp[i][-1] then
        tales_max_idx = i
    end
end

ans = -(1 << 40)

if k <= n then
    n.times do |i|
        tmp = dp[i].size > k ? k : dp[i].size
        (1..tmp).each do |j|
            ans = dp[i][j] if ans < dp[i][j]
        end
    end
    puts ans
elsif tales_max == 0 then
    n.times do |i|
        tmp = dp[i].size > n ? n : dp[i].size
        (1...tmp).each do |j|
            ans = dp[i][j] if ans < dp[i][j]
        end
    end
    puts ans
else
    tmp = ans
    sub_idx = 0
    n.times do |i|
        next if tales_max != dp[i][-1]
        sabun = (k % (dp[i].size-1)) + 1
        sabun.times do |j|
            ans = dp[i][j] if ans < dp[i][j]
        end
        if ans > tmp then
            tmp = ans
            sub_idx = i
        end
    end
    puts ans + tales_max * (k/(dp[sub_idx].size-1))
end