#[macro_export] macro_rules! setup { { mut $input:ident: SplitWhitespace $(,)? } => { use std::io::Read; let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).ok(); let mut $input = buf.split_whitespace(); }; } #[macro_export] macro_rules! parse_next { ($str_iter:expr) => { $str_iter.next().unwrap().parse().ok().unwrap() }; } use std::collections::HashMap; fn main() { setup! { mut input: SplitWhitespace }; let n: usize = parse_next!(input); let m: usize = parse_next!(input); let mut board = HashMap::new(); (0..m).for_each(|_| { let i: usize = parse_next!(input); let j: usize = parse_next!(input); let s: usize = parse_next!(input); board.insert((i, j), s); }); let ans = (|| { let mut dp = vec![0; 1 << n]; for src in 0..(1 << n) { for i in 0..n { if (src >> i) & 1 == 0 { let mut score = dp[src]; for j in 0..n { if (src >> j) & 1 == 1 { if let Some(s) = board.get(&(i, j)) { score += s; } } } let dest = src | (1 << i); dp[dest] = dp[dest].max(score); } } } dp[(1 << n) - 1] })(); println!("{}", ans); }