let nk = readLine()!.split(separator: " ").map{Int($0)!}
let pArray = readLine()!.split(separator: " ").map{Int($0)!}
let cArray = readLine()!.split(separator: " ").map{Int($0)!}
var graph = [Set<Int>: Int]() // cyclic set: total score of cycle
var setFromOneToN = Set<Int>(1...nk[0])
while !setFromOneToN.isEmpty {
    if let i = setFromOneToN.popFirst() {
        var set = Set<Int>()
        var score = cArray[i-1]
        set.insert(i)
        var nextPosition = pArray[i-1]
        while i != nextPosition {
            setFromOneToN.remove(nextPosition)
            set.insert(nextPosition)
            score += cArray[nextPosition-1]
            nextPosition = pArray[nextPosition-1]
        }
        graph[set] = score
    }
}
print(graph)

var maxCountGlobal = [Int]()
for (key, value) in graph {
    let keyCount = key.count
    let devided = (nk[1]-1) / keyCount
    let remainder = (nk[1]-1) % keyCount
    var maxCountLocal = [Int]()
    for start in key {
        var score = cArray[start-1]
        var maxScore = score
        var nextPosition = pArray[start-1]
        var it = 1
        while it <= remainder {
            score += cArray[nextPosition-1]
            if score > maxScore {
                maxScore = score
            }
            nextPosition = pArray[nextPosition-1]
            it += 1
        }
        if value > 0 {
            maxScore += devided * value
        }
        maxCountLocal.append(maxScore)
    }
    maxCountGlobal.append(maxCountLocal.max()!)
}
print(maxCountGlobal.max()!)
