[Solution] Bricks and Bags Codeforces Solution
There are bricks numbered from to . Brick has a weight of .
Pak Chanek has bags numbered from to that are initially empty. For each brick, Pak Chanek must put it into one of the bags. After this, each bag must contain at least one brick.
After Pak Chanek distributes the bricks, Bu Dengklek will take exactly one brick from each bag. Let be the weight of the brick Bu Dengklek takes from bag . The score is calculated as , where denotes the absolute value of .
It is known that Bu Dengklek will take the bricks in such a way that minimises the score. What is the maximum possible final score if Pak Chanek distributes the bricks optimally?
Each test contains multiple test cases. The first line contains an integer () — the number of test cases. The following lines contain the description of each test case.
The first line of each test case contains an integer () — the number of bricks.
The second line of each test case contains integers () — the weights of the bricks.
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, output a line containing an integer representing the maximum possible final score if Pak Chanek distributes the bricks optimally.
In the first test case, one way of achieving a final score of is to do the following:
- Put bricks , , and into bag .
- Put brick into bag .
- Put brick into bag .
If Pak Chanek distributes the bricks that way, a way Bu Dengklek can take the bricks is:
- Take brick from bag .
- Take brick from bag .
- Take brick from bag .
The score is . It can be shown that Bu Dengklek cannot get a smaller score from this distribution.
It can be shown that there is no other distribution that results in a final score bigger than .
No comments:
Post a Comment