n,k = gets.chomp.split(" ").map(&:to_i)
p = gets.chomp.split(" ").map(&:to_i)
c = gets.chomp.split(" ").map(&:to_i)
ans = -10000000000
1.upto(n) do |i|
  now = i # next = p[now]
  visited = Hash.new(0)
  point = 0
  cnt = 0
  max = -10000000000
  sp = Array.new(n+1,0)
  while visited[p[now-1]] == 0 && cnt < k
    point += c[p[now-1]-1]
    visited[p[now-1]] = 1
    now = p[now-1]
    cnt += 1
    sp[cnt] = point
    max = [point,max].max
  end
  if cnt < k && point > 0
    restcnt = k-cnt
    cycle = restcnt / cnt
    point = point + cycle * point
    restcnt = restcnt - cycle * cnt
    point += sp[0..restcnt].max
    max = [point,max].max
  end
  ans = [max,ans].max
end
puts ans
