n, k = gets.split.map(&:to_i)
p = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)

if c.max < 0
  puts c.max
  exit
end

loops = []
done = []

for i in 0..(n-1)
  next if done.include?(i)
  j = i
  scores = [c[p[j]-1]]
  while p[j]-1 != i
    j = p[j]-1
    scores << c[p[j]-1]
    done << j
  end
  loops << scores
end

ans = loops.map do |scores|
  score = [0, (k / scores.size) * scores.sum].max
  scoresscores = scores + scores
  extra = (0..(scores.size-2)).map do |len|
    tmp = (0..(scores.size-1)).map do |start|
      scoresscores[start..(start+len)].sum
    end.max
    if len+1 > k
      0
    elsif len+1 > k % scores.size && score > 0
      tmp - score
    else
      tmp
    end
  end.max
  extra = [0, extra].max
  score + extra
end.max

puts ans
