#include #include template std::vector vec(int len, T elem) { return std::vector(len, elem); } void solve() { int n; std::cin >> n; auto ps = vec(n, vec(3, 0)); for (auto& p : ps) { for (auto& x : p) { std::cin >> x; } } auto judge = [&](int ai, int aj, int bi, int bj) { int ax = ps[ai][(aj + 1) % 3], ay = ps[ai][(aj + 2) % 3]; int bx = ps[bi][(bj + 1) % 3], by = ps[bi][(bj + 2) % 3]; if (ax > ay) std::swap(ax, ay); if (bx > by) std::swap(bx, by); return bx <= ax && by <= ay; }; auto dp = vec(1 << n, vec(n, vec(3, -1))); for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { dp[1 << i][i][j] = ps[i][j]; } } int ans = 0; for (int b = 0; b < (1 << n); ++b) { for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { if (dp[b][i][j] == -1) continue; ans = std::max(ans, dp[b][i][j]); for (int ni = 0; ni < n; ++ni) { if ((b >> ni) & 1) continue; int nb = b | (1 << ni); for (int nj = 0; nj < 3; ++nj) { if (!judge(i, j, ni, nj)) continue; dp[nb][ni][nj] = std::max(dp[nb][ni][nj], dp[b][i][j] + ps[ni][nj]); } } } } } std::cout << ans << std::endl; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }