#define _USE_MATH_DEFINES #include using namespace std; int dp[1 << 16][16][6]; vector> rot = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}; vector> a; bool canPut (int rb, int ru, int ib, int iu) { bool ok = true; for (int i = 0; i < 2; i++) { ok &= a[ib][rot[rb][i]] >= a[iu][rot[ru][i]]; } return ok; } signed main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { int x, y, z; cin >> x >> y >> z; a.push_back({x, y, z}); } memset(dp, -1, sizeof(dp)); for (int i = 0; i < n; i++) { for (int j = 0; j < 6; j++) { dp[1 << i][i][j] = a[i][rot[j][2]]; } } for (int mask = 0; mask < (1 << n); mask++) for (int top = 0; top < n; top++) for (int r = 0; r < 6; r++) { if (dp[mask][top][r] == -1) continue; for (int i = 0; i < n; i++) if (!((mask >> i) & 1)) { for (int j = 0; j < 6; j++) { if (canPut(r, j, top, i)) { dp[mask | (1 << i)][i][j] = max(dp[mask | (1 << i)][i][j], dp[mask][top][r] + a[i][rot[j][2]]); } } } } int ans = 0; for (int i = 0; i < (1 << n); i++) for (int j = 0; j < n; j++) for (int k = 0; k < 6; k++) { ans = max(ans, dp[i][j][k]); } cout << ans << endl; return 0; }