AmazonOA #304
Find Minimum Pen Drive Capacity
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyTwo Pointers
Assign all games to exactly k children; each child gets at least one and at most two games, and each game goes to only one child. Every child has the same storage capacity. Find the minimum capacity that makes the assignment possible, that is, minimize the maximum total size of the games any child receives.
Input
The first line contains n k and the second line contains the game sizes. This site adds 1≤n,k≤200000, 1≤size[i]≤10^9. Unsolvable inputs are not excluded, and this site specifies the output −1 when there is no solution.
Output
Output a single integer, the answer.
Example 1
Input:
4 3
9 2 4 6
Output:
9
Example 2
Input:
4 2
8 3 5 4
Output:
11
Example 3
Input:
3 1
1 2 3
Output:
-1
Example notes
The answers are 9, 11 and −1. In the first example 2 and 4 form one group, and 6 and 9 each form a group of their own; pairing the largest game 9 is not optimal. Source 304 contains a placeholder loop that never terminates, and 305 also pairs the large games incorrectly; this site's code is original.
Editorial
Loading solution…