//use itertools::Itertools;
use std::cmp;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::io::Read;
use std::usize::MAX;

macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() };input_inner!{next, $($r)*}};($($r:tt)*) => {let stdin = std::io::stdin();let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));let mut next = move || -> String{bytes.by_ref().map(|r|r.unwrap() as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect()};input_inner!{next, $($r)*}};}
macro_rules! input_inner {($next:expr) => {};($next:expr, ) => {};($next:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($next, $t);input_inner!{$next $($r)*}};}
macro_rules! read_value {($next:expr, ( $($t:tt),* )) => {( $(read_value!($next, $t)),* )};($next:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()};($next:expr, chars) => {read_value!($next, String).chars().collect::<Vec<char>>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, [ $t:tt ]) => {{let len = read_value!($next, usize);(0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()}};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};}

fn solve() {
    input! {
        n: usize,
        k: usize,
        p:[usize1;n],
        c: [i64;n]
    }

    let mut total_score = vec![std::i64::MIN; n];
    let mut total_loop = vec![0; n];
    let mut way_score = vec![vec![std::i64::MIN; n + 1]; n];

    for i in 0..n {
        let mut count = 1;
        let mut now = p[i];
        let mut score = c[now];
        way_score[i][count] = score;

        while now != i {
            count += 1;
            now = p[now];
            score += c[now];
            way_score[i][count] = cmp::max(score, way_score[i][count - 1]);
        }

        total_score[i] = score;
        total_loop[i] = count;
    }

    let mut ans = std::i64::MIN;

    for i in 0..n {
        if k > total_loop[i] {
            if total_score[i] > 0 {
                let rest = k % total_loop[i];
                let loop_count = k / total_loop[i];
                let mut total = total_score[i] * loop_count as i64;
                total += way_score[i][rest];
                ans = cmp::max(ans, total);

                let mut total2 = total_score[i] * (loop_count - 1) as i64;
                total2 += way_score[i][total_loop[i]];
                ans = cmp::max(ans, total2);
            } else {
                ans = cmp::max(ans, way_score[i][total_loop[i]]);
            }
        } else {
            ans = cmp::max(ans, way_score[i][k]);
        }
    }

    //println!("{:?}", total_loop);
    println!("{}", ans);
}

fn main() {
    let thd = std::thread::Builder::new().stack_size(104_857_600);
    thd.spawn(|| solve()).unwrap().join().unwrap();

    /*
    // 入力を一括で読み込む場合
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut input = buf.split_whitespace();

    // inputに対しnext()で読み込んでいく
    let q: usize = input.next().unwrap().parse().unwrap();
    */
}

const LARGE_PRIME: u64 = 1_000_000_007;

// @〜でsnippet


Measured memory_bytes: 196748