SalesforceOA #16
Get Max Efficiency
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Assign every server exactly once to batches of the given sizes; the efficiency of a batch is its maximum capacity − its minimum capacity, and a single-element batch has efficiency 0. Find the maximum total efficiency. This definition and the rule that each element is used exactly once were recovered from the original HTML.
Input
The first line contains n k, the second line contains n capacity values, and the third line contains k numServers values. 1≤n≤200000, 1≤k≤n, capacities are in 1..10⁹, and every group size is ≥1, with the sizes summing to n.
Output
Output the maximum total efficiency, using a 64-bit integer.
Example 1
Input:
4 2
3 6 1 2
1 3
Output:
5
Example 2
Input:
4 1
1 2 3 4
4
Output:
3
Example 3
Input:
3 3
4 2 1
1 1 1
Output:
0
Example notes
The answers are 5, 3, and 0.
Editorial
Loading solution…