local mfl, mce = math.floor, math.ceil
local n, k = io.read("*n", "*n")
assert(-1000000000 <= k and k <= 1000000000)
local p, c = {}, {}
for i = 1, n do
  p[i] = io.read("*n")
end
for i = 1, n do
  c[i] = io.read("*n")
  assert(-1000000000 <= c[i] and c[i] <= 1000000000)
end
local asked = {}
for i = 1, n do
  asked[i] = false
end
local zpos = 1
local function findstart()
  for i = zpos, n do
    if not asked[i] then zpos = i + 1 return i end
  end
  return false
end
local ret = -1000000007LL
local function mma_l(a, b) return a < b and b or a end
while true do
  local spos = findstart()
  if not spos then break end
  local route = {spos}
  local sum = 0LL + c[spos]
  local src = spos
  asked[spos] = true
  while true do
    local dst = p[src]
    if asked[dst] then break end
    asked[dst] = true
    table.insert(route, dst)
    sum = sum + c[dst]
    src = dst
  end
  local len = #route
  if 0LL < sum then
    local lp = mfl(k / len)
    local rem = k
    if 2 <= lp then
      lp = lp - 1
      rem = k - lp * len
    end
    sum = sum * lp
    ret = mma_l(ret, sum)
    for i = 1, len do
      local v = 0LL
      local src = route[i]
      for j = 1, rem do
        local dst = p[src]
        v = v + c[dst]
        ret = mma_l(ret, sum + v)
        src = dst
      end
    end
  else
    for i = 1, len do
      local v = 0LL
      local src = route[i]
      local lim = math.min(len, k)
      for j = 1, lim do
        local dst = p[src]
        v = v + c[dst]
        ret = mma_l(ret, v)
        src = dst
      end
    end
  end
end
ret = tostring(ret):gsub("LL", "")
print(ret)
