Repovive
  • Dashboard
  • Ranking
  • Submissions
  • Discussion
  1. Contests
  2. Standard Round 8
  3. 26A

My submissions

/
/
/
/

Announcements

My submissions

/
/
/
/

Announcements

Solve
Author

26A. Opening the Bags

500 pts·2s·256 MB

Constraints

Input

Output

Samples

Input
t
2
Case 1
4
2 9 4 7
Case 2
4
1 3 5 7
Explanation
Case 1
Case 2
Output
Case 1
12
Case 2
8
Input
t
3
Case 1
1
5
Case 2
3
4 6 8
Case 3
5
2 3 8 1 4
Explanation
Case 1
Case 2
Case 3
Output
Case 1
3
Case 2
9
Case 3
10

Alice and Bob have nnn bags of candies, described by an array a1,a2,…,ana_1, a_2, \ldots, a_na1​,a2​,…,an​: bag iii contains aia_iai​ candies.

Alice first arranges all the bags in any order. They then open the bags in that order, taking turns, starting with Alice.

Whenever a bag is opened, its candies are divided as evenly as possible between Alice and Bob. If there is one extra candy, the person who opened that bag receives it.

All bags must be opened. Candies from different bags cannot be combined before they are divided.

For each test case, find the maximum total number of candies Alice can receive.

  • 1≤t≤1031 \leq t \leq 10^31≤t≤103
  • 1≤n≤1001 \leq n \leq 1001≤n≤100
  • 1≤ai≤1061 \leq a_i \leq 10^61≤ai​≤106
  • The sum of nnn over all test cases is at most 10510^5105.
ttt na1a2…an}×t\left. \begin{array}{l} n \\ a_1 \quad a_2 \quad \ldots \quad a_n \end{array} \right\} \times tna1​a2​…an​​}×t
answer}×t\left. \begin{array}{l} \mathrm{answer} \end{array} \right\} \times tanswer​}×t

Alice can arrange the bags as 9,2,7,49,2,7,49,2,7,4. She opens the first and third bags and receives 5+1+4+2=125+1+4+2=125+1+4+2=12 candies in total. This is the maximum possible.

With the bags in the given order, Alice receives 1+1+3+3=81+1+3+3=81+1+3+3=8 candies. Every ordering gives her the same total in this case.

Alice opens the only bag. She receives 333 candies, and Bob receives 222.

Every bag can be split equally. Alice receives 2+3+4=92+3+4=92+3+4=9 candies, regardless of the order.

Alice can arrange the bags as 3,2,1,8,43,2,1,8,43,2,1,8,4. She receives 2+1+1+4+2=102+1+1+4+2=102+1+1+4+2=10 candies in total, which is the maximum possible.