#P16212. [ECUSTPC 2025] 秋色堡垒
[ECUSTPC 2025] 秋色堡垒
Problem Description
Maddy has arrived in front of the fortress in autumn. She collected ingredients, whose freshness values are .
Maddy will use these ingredients to cook dinners for several days. In particular, since she only has two recipes with skill values and , she will cook exactly two dishes every day, not one less.
The cooking process for each day is as follows:
- Maddy may pick two different ingredients that have not been picked before, in any order. Let their freshness values be and . Then she cooks them, assigning one recipe to each ingredient. The deliciousness values of the two dishes are and , so the total deliciousness of that day's dinner is . Note that Maddy may assign the two ingredients to the two recipes in any order, and each day's dinner must use 2 different ingredients to make 2 dishes.
Please help Maddy choose how many days to cook dinner, and compute the maximum possible total deliciousness over those days. (Of course, if everything tastes terrible, she can also choose not to cook at all, i.e., the number of days can be .)
Input Format
The first line contains an integer (), the number of test cases.
For each test case, the first line contains three integers (), representing the number of ingredients and the skill values of the two recipes.
The next line contains integers (), representing the freshness of each ingredient.
It is guaranteed that over all test cases.
Output Format
For each test case, output one integer in one line, representing the maximum possible total deliciousness over those days.
6
4 1 2
4 5 6 7
5 2 8
0 -1 -2 -6 7
6 -1 -2
1 2 3 4 5 6
6 3 4
-1 0 0 1 2 3
6 -3 -4
1 0 0 -1 -2 -3
5 1 -10
-9 2 -5 3 -7
35
56
0
23
23
165
Hint
Explanation for Sample 1
For the 1st sample, we consider cooking dinner for 2 days:
- On day 1, choose ingredients with freshness and , and assign them to the recipes with skill values and , respectively. The dinner has deliciousness ;
- On day 2, choose ingredients with freshness and , and assign them to the recipes with skill values and , respectively. The dinner has deliciousness .
The total deliciousness is .
For the 2nd sample, we consider cooking dinner for 1 day: choose ingredients with freshness and , and assign them to the recipes with skill values and , respectively. The dinner has deliciousness .
For the 3rd sample, we consider not cooking dinner, because in any case the deliciousness of the dinner is negative.
Translated by ChatGPT 5