const main = (input) => {
  input = input.trim().split('\n')
  const [n, k] = input[0].split(' ').map(Number)
  const direc = input[1].split(' ').map(v => Number(v) - 1)
  const score = input[2].split(' ').map(Number)

  let max = -Infinity

  for (let i=0; i<n; i++) {
    const his = []
    const s = []
    let cur = i
    while (true) {
      const n = direc[cur]
      if (his.includes(n)) break
      his.push(n)

      const sc = score[n]
      s.push(sc)

      cur = n
    }

    let ac = 0
    for (let x=0, ii=0; x<k; x++) {
      ac += s[ii]
      max = Math.max(max, ac)

      ii = ii+1 <= s.length-1 ? ii+1 : 0
    }
  }

  return console.log(max)
}

process.env.MYTEST ? test() : main(require('fs').readFileSync('/dev/stdin', 'utf8'))