import java.util.*
import kotlin.math.absoluteValue
import kotlin.math.max

fun dfs(current: Int, next: IntArray, searched: BooleanArray, loop: MutableList<Int> = mutableListOf()): List<Int> {
    if (searched[current]) return loop
    loop.add(current)
    searched[current] = true
    return dfs(next[current], next, searched, loop)
}
fun calMaxScoreWithMaxLength(maxLength: Int, loop: List<Int>, scores: LongArray): Long {
    if (maxLength == 0) return 0L
    val queue = ArrayDeque<Pair<Int, Long>>()
    queue.addLast(-1 to 0L)
    var sum = 0L
    var result = Long.MIN_VALUE
    for (i in 0 until (loop.size shl 1)) {
        sum += scores[loop[i % loop.size]]
        while (queue.isNotEmpty() && queue.first.first + maxLength < i) {
            queue.pollFirst()
        }
        val (_, min) = queue.first
        if (result < sum - min) {
            result = sum - min
        }
        while (queue.isNotEmpty() && queue.last.second >= sum) {
            queue.pollLast()
        }
        queue.addLast(i to sum)
    }
    return result
}
fun calMaxScore(k: Int, loop: List<Int>, scores: LongArray): Long {
    return if (k >= loop.size) {
        val sum = loop.map(scores::get).sum()
        if (sum > 0) {
            val rest = k % loop.size
            val maxRest = calMaxScoreWithMaxLength(rest, loop, scores)
            val maxFull = calMaxScoreWithMaxLength(loop.size, loop, scores)
            max(sum * (k / loop.size) + maxRest, sum * (k / loop.size - 1) + maxFull)
        }else {
            calMaxScoreWithMaxLength(loop.size, loop, scores)
        }
    }else {
        calMaxScoreWithMaxLength(k, loop, scores)
    }
}
fun main() {
    val (n, k) = readLine()!!.trim().split(' ').map(String::toInt)
    val nexts = readLine()!!.trim().split(' ').map{it.toInt() - 1}.toIntArray()
    val scores = readLine()!!.trim().split(' ').map(String::toLong).toLongArray()
    val searched = BooleanArray(n){false}
    var maxScore = Long.MIN_VALUE
    for (i in 0 until n) {
        if (searched[i]) continue
        val loop = dfs(i, nexts, searched)
        val score = calMaxScore(k, loop, scores)
        maxScore = max(maxScore, score)
    }
    println(maxScore)
}