struct Scanner {
    private var elements = [String]()
    private var index = 0

    mutating func peek() -> String {
        while elements.count == index {
            elements = readLine()!.split(separator: " ").map(String.init)
            index = 0
        }
        return elements[index]
    }

    mutating func next() -> String {
        let value = peek()
        index += 1
        return value
    }

    mutating func nextInt() -> Int {
        return Int(next())!
    }

    mutating func nextDouble() -> Double {
        return Double(next())!
    }
}

var scanner = Scanner()
let N = scanner.nextInt()
let K = scanner.nextInt()
let P = (0 ..< N).map { _ in scanner.nextInt() - 1 }
let C = (0 ..< N).map { _ in scanner.nextInt() }

var answer = Int.min

for i in 0 ..< N {
    var k = i
    var cycleLength = 0
    var scorePerCycle = 0
    repeat {
        k = P[k]
        scorePerCycle += C[k]
        cycleLength += 1
        if cycleLength <= K {
            answer = max(answer, scorePerCycle)
        }
    } while k != i
    var score = (K / cycleLength) * scorePerCycle
    answer = max(answer, score)
    for _ in 0 ..< K % cycleLength {
        k = P[k]
        score += C[k]
        answer = max(answer, scorePerCycle)
    }
    answer = max(answer, score)
}

print(answer)
