#[allow(unused_macros)] macro_rules! read { ([$t:ty] ; $n:expr) => ((0..$n).map(|_| read!([$t])).collect::>()); ($($t:ty),+ ; $n:expr) => ((0..$n).map(|_| read!($($t),+)).collect::>()); ([$t:ty]) => (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::>()); ($t:ty) => (rl().parse::<$t>().unwrap()); ($($t:ty),*) => {{ let buf = rl(); let mut w = buf.split_whitespace(); ($(w.next().unwrap().parse::<$t>().unwrap()),*) }}; } #[allow(dead_code)] fn rl() -> String { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().to_owned() } fn main() { let n = read!(usize); let c = read!(usize); let _v = read!(usize); let ss = read!([usize]); let ts = read!([usize]); let ys = read!([usize]); let ms = read!([usize]); let max_time = 1000 * n; let mut v = ss .into_iter() .zip(ts.into_iter()) .zip(ys.into_iter()) .zip(ms.into_iter()) .map(|(((s, t), y), m)| (s, t, y, m)) .collect::>(); v.sort(); let mut memo = vec![vec![max_time; c + 1]; n + 1]; for y in 0..=c { memo[1][y] = 0; } for (s, t, y, m) in v.into_iter() { for sy in y..=c { let candidate = memo[s][sy] + m; for ty in 0..=(sy - y) { memo[t][ty] = memo[t][ty].min(candidate); } } } if memo[n][0] == max_time { println!("-1"); } else { println!("{}", memo[n][0]); } }