AmazonOA #261
Maximize Cluster Power
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Choose some disjoint triples of servers; each group's power is the median of its three values. Any server may go unused, and building no group at all is also allowed; maximize the sum of the groups' medians.
Input
The first line contains n; the second line contains the powers. The original text had no numeric bounds; this site uses 1≤n≤200000, −10^9≤power≤10^9; this site includes negative powers, and groups with negative gain need not be built.
Output
Output a single integer, the answer.
Example 1
Input:
6
1 2 3 4 5 6
Output:
8
Example 2
Input:
3
-3 -2 -1
Output:
0
Example 3
Input:
4
8 8 1 1
Output:
8
Example notes
The answers are 8, 0 and 8. With negative values, building no group gives 0 instead of being forced to build one.
Editorial
Loading solution…