use proconio::{input, marker::Usize1}; fn main() { input! { n:usize, m:usize, a:[i64;n], abc:[(Usize1,Usize1,i64);m], } let mut g = vec![vec![]; n]; let mut rg = vec![vec![]; n]; for &(u, v, w) in abc.iter() { g[u].push((v, w - a[u])); rg[v].push(u); } let mut vis = vec![false; n]; f(n - 1, &mut vis, &rg); const INF: i64 = std::i64::MAX; let mut dists = vec![INF; n]; dists[0] = 0; for i in 1..=n { for j in 0..n { for &(k, w) in g[j].iter() { if !vis[j] || !vis[k] { continue; } if dists[j] != INF && dists[k] > dists[j] + w { dists[k] = dists[j] + w; if i == n { println!("inf"); return; } } } } } let ans = -abc .iter() .filter(|&&(_, v, _)| v == n - 1) .map(|&(u, v, w)| dists[u] + w - a[u] - a[v]) .min() .unwrap(); println!("{}", ans); } fn f(crr: usize, vis: &mut Vec, g: &Vec>) { vis[crr] = true; for &nxt in g[crr].iter() { if vis[nxt] { continue; } f(nxt, vis, g); } }