P14361 [CSP-S 2025] Club Recruitment
Description
Little L is a member of the school's algorithm association. In this year's school club recruitment, Little L recruited $n$ new members in total, where $n$ is **even**. Now Little L wants to assign them to different departments of the association.
The algorithm association has three departments in total, where the satisfaction of the $i$-th new member ($1 \leq i \leq n$) with the $j$-th department ($1 \leq j \leq 3$) is $a_{i,j}$. The satisfaction of an assignment scheme is defined as the sum of the satisfactions of all new members with the departments they are assigned to. That is, if the $i$-th new member ($1 \leq i \leq n$) is assigned to the $d_i$-th department ($d_i \in \{1,2,3\}$), then the satisfaction of this assignment scheme is $\sum_{i=1}^{n} a_{i,d_i}$.
Little L does not want any department to have too many new members. Specifically, he requires that in the assignment scheme, no department is assigned more than $\frac{n}{2}$ new members. You need to help Little L compute the maximum satisfaction of an assignment scheme satisfying his requirements.
Input Format
This problem contains multiple test cases.
The first line of the input contains one positive integer $t$, which is the number of test cases.
Then the test cases are given one after another. For each test case:
- The first line contains one positive integer $n$, which is the number of new members.
- The $(i+1)$-th line ($1 \leq i \leq n$) contains three non-negative integers $a_{i,1}, a_{i,2}, a_{i,3}$, which are the satisfactions of the $i$-th new member with the 1st, 2nd and 3rd departments, respectively.
Output Format
For each test case, output one line with a non-negative integer, which is the maximum satisfaction of an assignment scheme satisfying Little L's requirements.
Explanation/Hint
### Sample 1 Explanation
This sample contains three test cases in total.
For the first test case, the four new members can be assigned to the 1st, 3rd, 1st and 2nd departments respectively. Then the numbers of new members in the three departments are $2,1,1$, all not exceeding $\frac{4}{2} = 2$, and the satisfaction is $4 + 4 + 5 + 5 = 18$.
For the second test case, the four new members can be assigned to the 1st, 1st, 2nd and 2nd departments respectively. Then the numbers of new members in the three departments are $2,2,0$, all not exceeding $\frac{4}{2} = 2$, and the satisfaction is $0 + 0 + 2 + 2 = 4$.
For the third test case, the two new members can be assigned to the 2nd and 1st departments respectively. Then the numbers of new members in the three departments are $1,1,0$, all not exceeding $\frac{2}{2} = 1$, and the satisfaction is $9 + 4 = 13$.
### Sample 2
See $\textbf{\textit{club/club2.in}}$ and $\textbf{\textit{club/club2.ans}}$ in the contestant's directory.
This sample satisfies the constraints of test cases $3,4$.
### Sample 3
See $\textbf{\textit{club/club3.in}}$ and $\textbf{\textit{club/club3.ans}}$ in the contestant's directory.
This sample satisfies the constraints of test cases $5 \sim 8$.
### Sample 4
See $\textbf{\textit{club/club4.in}}$ and $\textbf{\textit{club/club4.ans}}$ in the contestant's directory.
This sample satisfies the constraints of test case $9$.
### Sample 5
See $\textbf{\textit{club/club5.in}}$ and $\textbf{\textit{club/club5.ans}}$ in the contestant's directory.
This sample satisfies the constraints of test cases $15,16$.
### Constraints
For all testdata, it is guaranteed that:
- $1 \leq t \leq 5$;
- $2 \leq n \leq 10^5$, and $n$ is even;
- For all $1 \leq i \leq n$ and $1 \leq j \leq 3$, $0 \leq a_{i,j} \leq 2 \times 10^4$.
::cute-table{tuack}
| Test case | $n=$ | Special property |
| :--: | :--: | :--: |
| $1$ | $2$ | None |
| $2$ | $4$ | ^ |
| $3, 4$ | $10$ | ^ |
| $5 \sim 8$ | $30$ | ^ |
| $9$ | $200$ | B |
| $10, 11$ | ^ | None |
| $12$ | $10^5$ | A |
| $13, 14$ | ^ | B |
| $15, 16$ | ^ | C |
| $17 \sim 20$ | ^ | None |
Special property A: For all $1 \leq i \leq n$, $a_{i,2} = a_{i,3} = 0$.
Special property B: For all $1 \leq i \leq n$, $a_{i,3} = 0$.
Special property C: For all $1 \leq i \leq n$ and $1 \leq j \leq 3$, $a_{i,j}$ is generated independently and uniformly at random in $[0, 2 \times 10^4]$.
Translated by QwenWork