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