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

groups = []
seen = {}

(0...n).each do |i|
  group = []
  cur = i
  until seen[cur]
    seen[cur] = true
    group << cs[cur]
    cur = ps[cur] - 1
  end
  groups.push(group) unless group.empty?
end

total_max = nil
groups.each do |group|
  total_sum = group.sum
  max = nil
  remain = ((k - 1) % group.length) + 1
  remain = group.length if remain == 0
  (1..remain).each do |len|
    sum = 0
    (0...len).each do |i|
      sum += group[i]
    end
    if max.nil? || sum > max
      max = sum
    end

    (0...(group.length - 1)).each do |i|
      sum = sum - group[i] + group[(len + i) % group.length]
      if max.nil? || sum > max
        max = sum
      end
    end
  end
  base = [group.sum, 0].max * ((k - 1) / group.length)
  real_max = base + max
  total_max = real_max if total_max.nil? || real_max > total_max
end
puts total_max

