// Input
let NK = readLine()!.split(separator: " ").compactMap { Int($0) }
let PN = readLine()!.split(separator: " ").compactMap { Int($0) }
let CN = readLine()!.split(separator: " ").compactMap { Int($0) }

var result = Int.min
var removedIndexs = [Int: Bool]()
for i in 0..<NK[0] {
    var graph = [Int]()

    var nextIndex = i
    while removedIndexs[nextIndex] != true {
        graph.append(CN[nextIndex])
        removedIndexs[nextIndex] = true
        nextIndex = PN[nextIndex] - 1
    }

    if graph.isEmpty {
        continue
    }

    let graphSum = graph.reduce(0) { $0 + $1 }

    let mod: Int
    if graphSum > 0 {
        mod = NK[1] % graph.count
    } else {
        mod = NK[1] >= graph.count ? graph.count : NK[1]
    }

    var modMax = graph.max()!
    let doubleArray = graph + graph
    var cumulativeArray = [doubleArray[0]]
    for i in 0..<doubleArray.count-1 {
        cumulativeArray.append(cumulativeArray.last! + doubleArray[i+1])
    }
    for i in 0..<graph.count {
        for j in 1..<mod+1 {
            modMax = max(modMax, cumulativeArray[i+j] - cumulativeArray[i])
        }
    }

    if graphSum > 0 {
        result = max(result, modMax + graphSum * (NK[1] / graph.count))
    } else {
        result = max(result, modMax)
    }
}
print(result)
