import kotlin.math.max

fun main(args: Array<String>) {
    val (n, k) = readIntList()
    val p = readIntList().map { it - 1 }
    val c = readLongList()

    var ans = Long.MIN_VALUE
    for (i in 0 until n) {
        val score = mutableListOf<Long>()
        var tmp = 0L
        var next = p[i]
        do {
            tmp += c[next]
            score.add(tmp)
            next = p[next]
        } while (next != i)
        tmp += c[next]
        score.add(tmp)

        var tmpAns = if (score.last() > 0) score.last() * (k / score.size - 1) else 0
        next = i
        repeat((k % score.size + score.size).toInt()) {
            next = p[next]
            tmpAns += c[next]
            ans = max(ans, tmpAns)
        }
    }

    println(ans)
}

// read
fun readString() = readLine()!!
fun readInt() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readStringList() = readLine()!!.split(" ")
fun readIntList() = readLine()!!.split(" ").map(String::toInt)
fun readLongList() = readLine()!!.split(" ").map(String::toLong)
