import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

object Solve {

  const val INF = (10E17).toLong()

  fun solve() {

    var (longn, k) = readLine()!!.split(' ').map { it.toLong() }

    val n = longn.toInt()

    val p = readLine()!!.split(' ').map { it.toInt() - 1 }
    val c = readLine()!!.split(' ').map { it.toLong() }

    var ans = -INF

    for (i in 0 until n) {
      var cycleCnt = 0
      var cycleSum = 0L
      var now = i

      while (true) {
        cycleCnt++
        cycleSum += c[now]
        now = p[now]
        if (now == i) break
      }

      var cnt = 0
      var nowScore = 0L

      while (cnt <= cycleCnt) {
        cnt++
        now = p[now]
        nowScore += c[now]

        if(cnt > k) break

        val num = (k - cnt) / cycleCnt
        ans = max(ans, nowScore + max(0, num * cycleSum))
      }
    }

    println(ans)
  }
}

fun main(args: Array<String>) {
  Solve.solve()
}