import kotlin.math.max

fun main() {
    val (n, k) = readLine()!!.split(" ").map { it.toInt() }
    val p = listOf(0) + readLine()!!.split(" ").map { it.toInt() }
    val c = listOf(0L) + readLine()!!.split(" ").map { it.toLong() }

    val maximum = c.drop(1).max()!!
    if (maximum < 0) {
        println(maximum)
        return
    }

    val isUsed = Array(n + 1) { false }
    var res = 0L

    for (i in 1 until n + 1) {
        if (isUsed[i]) continue
        var current = i
        val loop = mutableListOf(i)
        while (p[current] != i) {
            current = p[current]
            loop.add(current)
        }
        val loopSum = loop.fold(0L) { acc, el -> acc + c[el] }

        for (ia in 0 until loop.count()) {
            val a = loop[ia]
            isUsed[a] = true
            var distAB = 0
            var costAB = -c[a]
            for (ib in ia until ia + loop.count()) {
                val b = loop[ib % loop.count()]
                costAB += c[b]
                val maxCost = costAB + max(0, loopSum) * ((k - distAB) / loop.count())
                res = max(res, maxCost)
                distAB += 1
                if (distAB > k) {
                    break
                }
            }
        }
    }

    println(res)
}
