inp = require('fs').readFileSync('/dev/stdin', 'utf8').trim().split('\n')
l = inp.shift().split(' ')
N = l.shift() * 1 
K = l.shift() * 1
P = inp.shift().split(' ').map(x => +x) 
C = inp.shift().split(' ').map(x => +x) 
a = []
b = []
for (i=0;i<N;i++) {
    a[i] = i
    b[i] = 0
}
f = true
cnt = 0
max = -10e9
while(cnt < Math.min(K, 5000)) {
    for (i=0;i<N;i++) {
        b[i] += C[P[a[i]] - 1]
        a[i] = P[a[i]] - 1
        if (b[i] > max) {
            max = b[i]
        }
    }
    cnt++
}
console.log(max)