object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n, k = sc.nextInt()
  val p = Array.fill(n)(sc.nextInt()-1)
  val c = Array.fill(n)(sc.nextLong())

  def decompose(a: Array[Int]): Array[Array[Int]] = {
    val n = a.length
    val visited = Array.fill(n)(false)
    var res = Array[Array[Int]]()

    for (i <- 0 until n) {
      var next = i
      var factor = Array[Int]()
      while (!visited(next)) {
        visited(next) = true
        factor :+= next
        next = a(next)
      }
      if (factor.nonEmpty) {
        res :+= factor
      }
    }

    res
  }

  val d = decompose(p).map(_.map(c))

  var ans = Long.MinValue
  for (f <- d) {
    val l = f.length
    val fa = (f ++ f).scanLeft(0L)(_ + _)
    val sum = fa(l) - fa(0)
    if (sum > 0) {
      val x = k / l
      val y = k % l
      val z = if (x == 0) 1 else 0
      for {
        i <- 0 until l
        j <- i+z to i+y
      } {
        ans = ans.max(sum * x + fa(j) - fa(i))
      }

      val w = if (x == 1) 1 else 0
      if (x != 0) {
        for {
          i <- 0 until l
          j <- i+w to i+l
        } {
          ans = ans.max(sum * (x-1) + fa(j) - fa(i))
        }
      }
    } else {
      for {
        i <- 0 until l
        j <- i+1 to i+(k.min(l))
      } {
        ans = ans.max(fa(j) - fa(i))
      }
    }
  }

  println(ans)
}
