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