AmazonOA #279
Maximum Quality Sum
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Every packet must be assigned to one of exactly channels non-empty channels. You may group them arbitrarily; a group does not need to be contiguous in the original order. The quality of a channel is the median of its packet sizes; with an even count, take the average of the two middle items. Maximize the total quality, then round it up to an integer once at the end.
Input
The first line contains n channels and the second line contains packets. 1≤n≤500000, 1≤packets[i]≤10^9, 1≤channels≤n.
Output
Output a single integer, the answer.
Example 1
Input:
5 2
1 2 3 4 5
Output:
8
Example 2
Input:
5 2
2 2 1 5 3
Output:
7
Example 3
Input:
3 3
89 48 14
Output:
151
Example notes
The answers are 8, 7 and 151 respectively. In the first example 5 is on its own, and the remaining 1,2,3,4 have median 2.5, so the total 7.5 rounds up to 8.
Editorial
Loading solution…