use std::io::Read; fn main() { // input let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut itr = s.trim().split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let c: usize = itr.next().unwrap().parse().unwrap(); let v: usize = itr.next().unwrap().parse().unwrap(); let s: Vec = (0..v) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let t: Vec = (0..v) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let y: Vec = (0..v) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let m: Vec = (0..v) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let mut g: Vec> = vec![Vec::new(); n + 1]; for i in 0..v { g[s[i] - 1].push((t[i] - 1, y[i], m[i])); } // calc let inf: u32 = 100000; let mut dp: Vec> = vec![vec![inf; 606]; n]; dp[0][0] = 0; for i in 0..n { for j in 0..g[i].len() { let nv = g[i][j].0; let ny = g[i][j].1; let nm = g[i][j].2; for py in 0..301 { dp[nv][py + ny] = std::cmp::min(dp[nv][py + ny], dp[i][py] + nm); } } } let mut ans: u32 = inf; for i in 0..c + 1 { ans = std::cmp::min(ans, dp[n - 1][i]); } if ans == inf { println!("-1"); } else { print!("{}", ans); } }