use std::io::Read; fn main() { let mut buf = String::new(); let mut stdin = std::io::stdin(); stdin.read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let n: usize = iter.next().unwrap().parse().unwrap(); let m: usize = iter.next().unwrap().parse().unwrap(); let mut board: Vec> = vec![vec![0; n]; n]; let mut row: usize = 0; while row < m { let front: usize = iter.next().unwrap().parse().unwrap(); let back: usize = iter.next().unwrap().parse().unwrap(); let score: usize = iter.next().unwrap().parse().unwrap(); board[front][back] = score; row += 1; } let mut max: usize = 0; for perm in permutation(n) { let mut total_score: usize = 0; let mut is_set: Vec = vec![false; n]; for num in perm { for b in (0..n).filter(|&b| !is_set[b]) { total_score += board[num][b]; } is_set[num] = true; } if total_score > max { max = total_score; } } println!("{}", max); } fn permutation(n: usize) -> Vec> { if n == 1 { return vec![vec![0]] } let mut vec: Vec> = Vec::new(); for p in permutation(n-1).iter_mut() { p.push(n-1); vec.push(p.clone()); for i in (1..n).rev() { p.swap(i, i-1); vec.push(p.clone()); } } vec }