PTSD
Time limit1sMemory limit1024 MB
Soldiers numbered 1 to n are split into groups so that the sum of power values of PTSD soldiers who are second strongest in their group is maximized.
- Level
Medium7 of 10
- Topics
- Greedy, Dynamic programming
- Solved
- No attempts yet
Problem
There are soldiers in the JB kingdom, numbered . The -th soldier has power value .
A tournament is held in the kingdom. The soldiers must be divided into several groups so that each soldier belongs to exactly one group. A group may contain only one soldier.
Some soldiers have post-traumatic stress disorder (PTSD). A soldier with PTSD is upset if exactly one other soldier in his group has a larger power value than him, that is, he is the second strongest in the group.
JB, the king, wants to maximize the sum of the power values of the upset soldiers. Divide the soldiers to find this maximum.
Input
The first line contains a positive integer , the number of test cases. For each test case:
The first line contains an integer (), the number of soldiers.
The second line contains a string of length consisting of '0' and '1'. The -th character describes the -th soldier. If is '1', the -th soldier has PTSD. Otherwise, the -th soldier does not have PTSD.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output one line containing the maximum sum of the power values of the upset soldiers.
Hint
In the first test case, a valid division is [1, 2], [3, 4], [5]. It makes soldiers 1 and 3 upset. The division [1, 2], [3, 5], [4] is also valid.
In the second test case, a valid division is [1, 2], [3, 4], [5, 6], [7, 8].
In the third test case, a valid division is [1, 3], [2, 4].
In the fourth test case, a valid division is [1, 2, 3, 4].