fn main() {
    ioset! { inp, buf }
    macro_rules! scan {
        ($($r:tt)*) => {
            input! { inp, $($r)* }
        }
    }
    macro_rules! print {
        ($($t:expr),*) => {
            write!(buf, $($t),*).unwrap();
        } 
    }
    scan! {
        n: usize,
        k: usize,
        p: [usize1; n],
        c: [i64; n],
    }
    let mut ans = std::i64::MIN;
    for s in 0..n {
        let mut i = s;
        let mut idx = vec![None; n];
        let mut sum = Vec::new();
        let mut now = 0;
        let mut ki = k;
        while ki > 0 {
            let next = p[i];
            match idx[next] {
                None => {
                    idx[next] = Some(sum.len());
                    sum.push(now);
                }
                Some(l) => {
                    let loop_cost = now - sum[l];
                    //print!("{} + {} - {}\n", now, c[next], sum[l]);
                    for j in l..sum.len() {
                        let kj = ki + (sum.len() - j);
                        let time = (kj / (sum.len() - l)) as i64;
                        //print!("{}: {} -> {} = {} + {} * {}\n", kj, i, next, sum[j], loop_cost, time);
                        let cost = sum[j] + loop_cost * time;
                        ans = std::cmp::max(ans, cost);
                    }
                    break;
                }
            }
            ki -= 1;
            now += c[next];
            i = next;
            ans = std::cmp::max(ans, now);
        }
    }
    print!("{}\n", ans);
}

use std::io::{ stdout, BufWriter, Write };

#[macro_export]
macro_rules! ioset {
    ($inp:ident, $buf:ident) => {
        inset!($inp);
        outset!($buf);
    }
}

#[macro_export]
macro_rules! inset {
    (source = $s:expr, $iter:ident) => {
        let mut $iter = $s.split_whitespace();
    };
    ($iter:ident) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut $iter = s.split_whitespace();
    }
}

#[macro_export]
macro_rules! input {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt, $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input! { $iter, $($r)* }
    };
    ($iter:expr, mut $var:ident : $t:tt, $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input! { $iter, $($r)* }
    };
    ($iter:expr, ($var:expr) : $t:tt, $($r:tt)*) => {
        $var = read_value!($iter, $t);
        input! { $iter, $($r)* }
    };
}

#[macro_export]
macro_rules! read_value {

    // tuple
    ($iter:expr, ( $($t:tt), * )) => {
        ( $(read_value!($iter, $t)), * )
    };
    
    // array
    ($iter:expr, [ $t:tt; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    
    // string
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        $iter.next().unwrap().parse::<usize>().expect("Parse error") - 1
    };
    
    // any other
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[macro_export]
macro_rules! outset {
    ($buf:ident) => {
        let sout = stdout();
        let mut $buf = BufWriter::new(sout.lock());
    }
}

#[macro_export]
macro_rules! output {
    ($buf:expr, $($t:expr),*) => {
        write!($buf, $($t),*).unwrap();
    }
}

struct Chars<'a>(&'a Vec<char>);

impl<'a> std::fmt::Display for Chars<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(f, "{}", self.0.iter().collect::<String>())
    }
}


Measured memory_bytes: 2416