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

fun intList() = readLine()?.split(" ")?.map(String::toInt) ?: TODO()
fun longList() = readLine()?.split(" ")?.map(String::toLong) ?: TODO()
fun main() {
    val (n, k) = longList()
    val p = intList().map { it - 1 }
    val c = longList()
    val maps = mutableListOf<Map<Int, Long>>()
    var m = Long.MIN_VALUE
    repeat(n.toInt()) { i ->
        var index = i
        val route = maps.find { it.containsKey(p[index]) } ?: run {
            val map = hashMapOf<Int, Long>()
            while (true) {
                val next = p[index]
                if (map.containsKey(next)) {
                    map[-1] = map.values.sum()
                    maps.add(map)
                    break
                } else {
                    index = next
                    map[next] = c[index]
                }
            }
            map
        }
        val routeSize = route.keys.size - 1
        val loop = (k - 1) / routeSize
        val seed = (route[-1] ?: 0) * loop
        if (seed > 0) {
            val remain = ((k - 1) % routeSize).toInt() + 1
            var score = 0L
            index = i
            repeat(remain) {
                val next = p[index]
                index = next
                score += c[index]
                m = max(seed + score, m)
            }
            m = max(seed + score, m)
        } else {
            var score = 0L
            index = i
            repeat(min(k.toInt(), routeSize)) {
                val next = p[index]
                index = next
                score += c[index]
                m = max(score, m)
            }
            m = max(score, m)
        }
    }
    println(m.toString())
}
