import Foundation

class Scanner {
    static func readInt() -> Int {
        return Int(readLine()!)!
    }
    
    static func readIntArr() -> [Int] {
        return readLine()!.split(separator: " ").map { Int($0)! }
    }
    
    static func readM(_ n: Int) -> [[Int]] {
        var res = [[Int]]()
        for _ in 0..<n {
            res.append(readIntArr())
        }
        return res
    }
    
    static func readUknownIntMat() -> [[Int]] {
        var res = [[Int]]()
        while let line = readLine() {
            res.append(line.split(separator: " ").map { Int($0)! })
        }
        return res
    }
    
    static func readString() -> String {
        return readLine()!
    }
}

class Printer {
    static func printIntArray(_ arr: [Int]) {
        print(arr.map { String($0) }.joined(separator: " "))
    }
}


class Solution {
    var arr: [Int]!
    var graph: [Int]!
    var visited: [Bool]!
    var k = 0
    
    func find(_ n: Int, _ k: Int, _ p: [Int], _ arr: [Int]) -> Int {
        self.k = k
        self.arr = arr
        self.visited = [Bool](repeating: false, count: n)
        self.graph = p.map { $0-1 }
        
        var res = -10000000000
        for i in stride(from: 0, to: n, by: 1) {
            guard !visited[i] else {
                continue
            }
            var series = [Int]()
            let cycle = dfs(i, &series)
            let cycleCount = cycle[0], cycleTotal = cycle[1]
            if k >= cycleCount {
                res = max(res, findMax(series, cycleCount))
            }
            res = max(res,
                      max(cycleTotal * (k/cycleCount), 0) + findMax(series, k%cycleCount)
            )
        }
        return res
    }
    
    
    func dfs(_ cur: Int, _ series: inout [Int]) -> [Int] {
        visited[cur] = true
        let n = graph[cur]
        series.append(arr[n])
        guard !visited[n] else {
            return [1, arr[n]]
        }
        let res = dfs(n, &series)
        return [1 + res[0], arr[n] + res[1]]
    }
    
    func findMax(_ series: [Int], _ rem: Int) -> Int {
        let cnt = series.count
        var res = -10000000000
        for start in stride(from: 0, to: cnt, by: 1) {
            var j = start
            var steps = rem, sum = 0
            while steps > 0 {
                if sum > 0 {
                    sum += series[j%cnt]
                } else {
                    sum = series[j%cnt]
                }
                res = max(res, sum)
                j += 1
                steps -= 1
            }
        }
        return res
    }
}



let nk = Scanner.readIntArr()
let n = nk[0], k = nk[1]
let p = Scanner.readIntArr()
let arr = Scanner.readIntArr()

print(Solution().find(n, k, p, arr))
