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 const INF: u32 = 100_000; let mut dp: Vec> = vec![vec![INF; 606]; n]; dp[0][0] = 0; for i in 0..n { for &j in g[i].iter() { let nv = j.0; let ny = j.1; let nm = j.2; for cy in 0..301 { dp[nv][cy + ny] = std::cmp::min(dp[nv][cy + ny], dp[i][cy] + 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 { println!("{}", ans); } }