import java.util.*

import java.util.*

fun main(args: Array<String>) {
  val sc = Scanner(System.`in`)
  val n = sc.nextInt()
  val k = sc.nextInt()
  val p = (1..n).map { sc.nextInt() - 1 }.toIntArray()
  val c = (1..n).map { sc.nextLong() }.toLongArray()


  val array = Array(n) { Pair(0, 0L) }
  for (i in 0 until n) {
    var now = i
    var count = 0
    var sum = 0L
    while (true) {
      now = p[now]
      count++
      sum += c[now]
      if (now == i) break
    }
    array[i] = Pair(count, sum)
  }

  var max = Long.MIN_VALUE
  for (i in 0 until n) {
    val ai = array[i]
    val size = ai.first
    val sum = ai.second

    val kk = k / size - 1
    val last = k % size

    var score = 0L
    if (sum > 0) {
      score += sum * kk
    }
    var now = i
    for (j in 0 until size + last) {
      now = p[now]
      score += c[now]
      max = Math.max(score, max)

    }

  }

  println(max)

}