n,k = gets.split.map(&:to_i)
pa = gets.split.map{|t|t.to_i-1}
ca = gets.split.map(&:to_i)

dbgp = 45.times.map{Array.new(n, 0)}
dbgc = 45.times.map{Array.new(n, 0)}

n.times{|i|
  dbgp[0][i] = pa[i]
  dbgc[0][i] = ca[pa[i]]
}

43.times{|i|
  n.times{|j|
    dbgp[i+1][j] = dbgp[i][dbgp[i][j]]
    dbgc[i+1][j] = dbgc[i][j]+dbgc[i][dbgp[i][j]]
  }
}

max = -(10**18)
n.times{|i|
  ans = 0
  now = i
  ([5005,k].min).times{|j|
    ans += ca[pa[now]]
    now = pa[now]
    max = ans if max < ans
  }
}

n.times{|i|
  ans = 0
  idx = 0
  t = k
  now = i
  while t>0
    if t%2==0
      ans += dbgc[idx][now]
      now = dbgp[idx][now]
    end
    t/=2
    idx+=1
  end
  max = ans if max < ans
}

p max
