//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // use std::io::Write; fn run() { input! { n: usize, p: [(i32, i32, i32); n], } let ini = std::i32::MIN; let mut dp = vec![vec![vec![ini; 1 << n]; n]; 3]; for (i, p) in p.iter().enumerate() { dp[0][i][1 << i] = p.0; dp[1][i][1 << i] = p.1; dp[2][i][1 << i] = p.2; } for bit in 1..(1 << n) { for i in 0..n { if bit >> i & 1 == 0 { continue; } let (a, b, c) = p[i]; for j in 0..n { if bit >> j & 1 == 1 { continue; } let (x, y, z) = p[j]; if (b >= y && c >= z) || (b >= z && c >= y) { let k = bit | (1 << j); dp[0][j][k] = std::cmp::max(dp[0][j][k], dp[0][i][bit] + x); } if (b >= x && c >= z) || (b >= z && c >= x) { let k = bit | (1 << j); dp[1][j][k] = std::cmp::max(dp[1][j][k], dp[0][i][bit] + y); } if (b >= y && c >= x) || (b >= x && c >= y) { let k = bit | (1 << j); dp[2][j][k] = std::cmp::max(dp[2][j][k], dp[0][i][bit] + z); } if (a >= y && c >= z) || (a >= z && c >= y) { let k = bit | (1 << j); dp[0][j][k] = std::cmp::max(dp[0][j][k], dp[1][i][bit] + x); } if (a >= x && c >= z) || (a >= z && c >= x) { let k = bit | (1 << j); dp[1][j][k] = std::cmp::max(dp[1][j][k], dp[1][i][bit] + y); } if (a >= x && c >= y) || (a >= y && c >= x) { let k = bit | (1 << j); dp[2][j][k] = std::cmp::max(dp[2][j][k], dp[1][i][bit] + y); } if (a >= y && b >= z) || (a >= z && b >= y) { let k = bit | (1 << j); dp[0][j][k] = std::cmp::max(dp[0][j][k], dp[2][i][bit] + x); } if (a >= x && b >= z) || (a >= z && b >= x) { let k = bit | (1 << j); dp[1][j][k] = std::cmp::max(dp[1][j][k], dp[2][i][bit] + y); } if (a >= x && b >= y) || (a >= y && b >= x) { let k = bit | (1 << j); dp[2][j][k] = std::cmp::max(dp[2][j][k], dp[2][i][bit] + y); } } } } let mut ans = 0; for a in dp { for a in a { for a in a { ans = std::cmp::max(ans, a); } } } println!("{}", ans); } fn main() { run(); }