#[allow(unused_macros)] macro_rules! read { ([$t:ty] ; $n:expr) => ((0..$n).map(|_| read!([$t])).collect::>()); ($($t:ty),+ ; $n:expr) => ((0..$n).map(|_| read!($($t),+)).collect::>()); ([$t:ty]) => (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::>()); ($t:ty) => (rl().parse::<$t>().unwrap()); ($($t:ty),*) => {{ let buf = rl(); let mut w = buf.split_whitespace(); ($(w.next().unwrap().parse::<$t>().unwrap()),*) }}; } #[allow(dead_code)] fn rl() -> String { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().to_owned() } fn main() { let (n, m) = read!(usize, usize); let table = read!([usize]; m); let mut max_score = 0; // let mut best_order: Vec = (0..n).collect(); let mut order: Vec = (0..n).collect(); loop { let mut score = 0; // calc point. for data in &table { let (item1, item2, s) = (data[0], data[1], data[2]); // find index of item1 let mut i1 = 0; for i in 0..n { if order[i] == item1 { i1 = i; break; } } for i in i1+1..n { if order[i] == item2 { score += s; break; } } } // update best order if score >= max_score { // best_order = order.clone(); max_score = score; } // update order if !order.next_permutation() { break; } } // println!("{:?}", best_order); println!("{}", max_score); } pub trait LexicalPermutation { /// Return `true` if the slice was permuted, `false` if it is already /// at the last ordered permutation. fn next_permutation(&mut self) -> bool; /// Return `true` if the slice was permuted, `false` if it is already /// at the first ordered permutation. fn prev_permutation(&mut self) -> bool; } impl LexicalPermutation for [T] where T: Ord { /// Original author in Rust: Thomas Backman fn next_permutation(&mut self) -> bool { // These cases only have 1 permutation each, so we can't do anything. if self.len() < 2 { return false; } // Step 1: Identify the longest, rightmost weakly decreasing part of the vector let mut i = self.len() - 1; while i > 0 && self[i-1] >= self[i] { i -= 1; } // If that is the entire vector, this is the last-ordered permutation. if i == 0 { return false; } // Step 2: Find the rightmost element larger than the pivot (i-1) let mut j = self.len() - 1; while j >= i && self[j] <= self[i-1] { j -= 1; } // Step 3: Swap that element with the pivot self.swap(j, i-1); // Step 4: Reverse the (previously) weakly decreasing part self[i..].reverse(); true } fn prev_permutation(&mut self) -> bool { // These cases only have 1 permutation each, so we can't do anything. if self.len() < 2 { return false; } // Step 1: Identify the longest, rightmost weakly increasing part of the vector let mut i = self.len() - 1; while i > 0 && self[i-1] <= self[i] { i -= 1; } // If that is the entire vector, this is the first-ordered permutation. if i == 0 { return false; } // Step 2: Reverse the weakly increasing part self[i..].reverse(); // Step 3: Find the rightmost element equal to or bigger than the pivot (i-1) let mut j = self.len() - 1; while j >= i && self[j-1] < self[i-1] { j -= 1; } // Step 4: Swap that element with the pivot self.swap(i-1, j); true } }