#[allow(unused_imports)] use std::cmp::*; use std::io::*; #[allow(dead_code)] fn getline() -> String { let mut ret = String::new(); std::io::stdin().read_line(&mut ret).ok(); return ret; } #[allow(dead_code)] fn getword() -> String { let mut stdin = std::io::stdin(); let mut u8b: [u8; 1] = [0]; loop { let mut buf: Vec = Vec::with_capacity(16); loop { let res = stdin.read(&mut u8b); if res.is_err() ||u8b[0] <= ' ' as u8 { break; } else { buf.push(u8b[0]); } } if buf.len() >= 1 { let ret = std::string::String::from_utf8(buf).unwrap(); return ret; } } } #[allow(dead_code)] fn parse(s : &str) -> T { return s.parse::().ok().unwrap(); } #[allow(dead_code)] fn get() -> T { parse(&getword()) } fn main() { let n: usize = get(); let m: usize = get(); let mut items: Vec<(usize, usize, i32)> = Vec::new(); for _ in 0 .. m { let a: usize = get::(); let b: usize = get::(); let c: i32 = get::(); items.push((a, b, c)) } let mut dp = vec![0; 1 << n]; for bits in 0 .. (1 << n) { for i in 0 .. n { if (bits & (1 << i)) == 0 { continue; } for ei in 0 .. m { let (f, t, s) = items[ei]; if t == i && (bits & (1 << f)) != 0 { dp[bits] = max(dp[bits], dp[bits ^ (1 << i)] + s); } } } } println!("{}", dp[(1 << n) - 1]); }