use proconio::{input, marker::Usize1}; fn main() { input! { n: usize, m: usize, p: usize, y: isize, abc: [(Usize1, Usize1, isize); m], de: [(Usize1, isize); p], } let mut graph = vec![vec![]; n]; for (a, b, c) in abc { graph[a].push((b, c)); graph[b].push((a, c)); } let mut stores = vec![isize::MAX; n]; for (d, e) in de { stores[d] = e; } let mut ans = 0; dfs(0, y, &mut ans, &mut vec![false; n], &graph, &stores); println!("{}", ans); } fn dfs( pos: usize, money: isize, ans: &mut isize, visited: &mut Vec, graph: &Vec>, stores: &Vec, ) { if money == 0 { return; } visited[pos] = true; *ans = (*ans).max(money / stores[pos]); for &(next, robbed) in &graph[pos] { if visited[next] { continue; } dfs(next, (money - robbed).max(0), ans, visited, graph, stores); } visited[pos] = false; }