#[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() }; } fn main() { setup! { mut input: SplitWhitespace }; let n: usize = parse_next!(input); let m: usize = parse_next!(input); let mut item1: Vec = vec![]; let mut item2: Vec = vec![]; let mut score: Vec = vec![]; (0..m).for_each(|_| { item1.push(parse_next!(input)); item2.push(parse_next!(input)); score.push(parse_next!(input)); }); let mut map = std::collections::HashMap::new(); for i in 0..m { map.insert((item1[i], item2[i]), score[i]); } let mut vec = vec![0]; for i in 1..n { let mut index = 0; let mut max = 0; for j in 0..=vec.len() { let mut score = 0; for k in 0..j { if let Some(value) = map.get(&(vec[k], i)) { score += value; } } for k in j..vec.len() { if let Some(value) = map.get(&(i, vec[k])) { score += value; } } if score >= max { index = j; max = score; } } if index == vec.len() { vec.push(i); } else { vec.insert(index, i); } } let ans = (|| { let mut score = 0; for i in 0..n { for j in i..n { if let Some(value) = map.get(&(vec[i], vec[j])) { score += value; } } } score })(); println!("{}", ans); }