// 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) }

// Make Graph
var graphs = [[Int]]()
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 {
        graphs.append(graph)
    }
}

// Output
let graphMaxes: [Int] = graphs.map { graph in
    let graphSum = graph.reduce(0) { $0 + $1 }

    // Calculate max in N % K
    let mod: Int
    if (graphSum > 0) && (NK[1] >= graph.count) {
        mod = NK[1] % graph.count
    } else {
        mod = NK[1] >= graph.count ? graph.count : NK[1]
    }
    var modMax = graph.max()!
    var tmpArray = graph
    for i in 0..<(mod-1) {
        for j in 0..<tmpArray.count {
            tmpArray[j] += graph[(i+j+1) % graph.count]
        }
        modMax = max(modMax, tmpArray.max()!)
    }

    if graphSum > 0 {
        return modMax + graphSum * (NK[1] / graph.count)
    } else {
        return modMax
    }
}
print(graphMaxes.max()!)
