#![allow(unused_imports)] use ac_library::{ModInt998244353 as mint, Segtree, dsu, modint}; use itertools::{Itertools, izip}; use num::Float; use proconio::{ fastout, input, marker::{Chars, Usize1}, }; use std::{ cmp::Reverse, collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet}, }; // #[fastout] fn main() { input! { n : usize, c : usize, m : usize, s : [Usize1; m], t : [Usize1; m], x : [usize; m], y : [i64; m], } let es = izip!(s, t, x, y).collect_vec(); let g = es.iter().fold(vec![vec![]; n], |mut acc, &(u, v, x, y)| { acc[u].push((v, x, y)); acc }); let inf = 1e18 as i64; let mut dp = vec![vec![inf; c + 1]; n]; dp[0][c] = 0; for i in 0..n { for &(to, x, y) in &g[i] { for j in x..=c { let mi = dp[to][j - x].min(dp[i][j] + y); dp[to][j - x] = mi; } } } let mut ans = *dp[n - 1].iter().min().unwrap(); if ans == inf { ans = -1; } println!("{}", ans); }