fun main() {
  val (N, K) = readInts()
  val P = readInts().map { it - 1 }
  val C = readInts()

  fun f(begin: Int): Long {
    val visited = Array(N) { false }
    val scores = Array(N) { Long.MIN_VALUE } // i ステップの時の score
    var p = begin
    visited[begin] = true
    var score = 0L
    var stepNum = 0
    for (i in 0 until K) {
      stepNum++
      visited[p] = true
      score += C[p]
      scores[i] = score
      p = P[p]
      if (visited[p]) break
    }
    val lastScore = scores[stepNum - 1]
    if (K <= stepNum || lastScore <= 0L) {
      return scores.max()!!
    } else {
      var loopNum = K / stepNum
      if (K % stepNum == 0) loopNum--
      val bonus = lastScore * loopNum
      val k = K - stepNum * loopNum
      return bonus + ((0 until k).map { scores[it] }.max() ?: 0L)
    }
  }

  val results = (0 until N).map { f(it) }
  println(results.max())
}

fun readString() = readLine()!!
fun readStrings() = readString().split(" ")
fun readInt() = readString().toInt()
fun readLong() = readString().toLong()
fun readInts() = readStrings().map { it.toInt() }
fun readLongs() = readStrings().map { it.toLong() }
