trait NextPermutation { fn next_permutation(&mut self) -> bool; } impl NextPermutation for Vec { fn next_permutation(&mut self) -> bool { self.windows(2) .rposition(|x| x[0] < x[1]) .map_or(false, |i| { let j = self.iter().rposition(|x| x > &self[i]).unwrap(); self.swap(i, j); self[i + 1..].reverse(); true }) } } fn main() { let mut xx = String::new(); std::io::Read::read_to_string(&mut std::io::stdin(), &mut xx).ok(); let xx: Vec = xx.split_whitespace().flat_map(str::parse).collect(); let n = xx[0]; let mut scores = vec![]; for x in xx[2..].chunks(3) { scores.push((x[0], x[1], x[2])); } let mut items: Vec = (0..n).collect(); let mut max = 0; loop { let mut idx = vec![0; n]; for (i, &item) in items.iter().enumerate() { idx[item] = i; } let mut score = 0; for &(i, j, s) in &scores { if idx[i] < idx[j] { score += s; } } max = max.max(score); if !items.next_permutation() { break; } } println!("{max}"); }