AmazonOA #307
Minimum Total Packaging Effort
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Each of the m items must be packed exactly once. Each center can be used at most once, or not at all. At center i, paying for at least packageCount[i] items gets you 2 more items for free, and the effort of each of those two free items must not exceed that of any paid item at the center. A center grants only this one set of 2 free items, and every other item must be paid for. You may pay at a center without claiming the offer, and you may also just pay without reaching the threshold. Find the minimum total paid effort for all items.
Input
The first line contains m n, the second line contains m packEffort values, and the third line contains n packageCount values. The original bounds are 1≤m,n≤100000, 0≤packEffort[i]≤10000. This site adds 1≤packageCount[i]≤10^9 and does not additionally require a threshold≤m.
Output
Output a single integer, the answer.
Example 1
Input:
4 1
10 9 8 1
1
Output:
19
Example 2
Input:
6 2
6 5 4 3 2 1
1 1
Output:
9
Example 3
Input:
3 1
0 0 0
7
Output:
0
Example notes
The answers are 19, 9 and 0. In the first example the only center must hold all four items, so only 8 and 1 can be free; you cannot pay 10, get 9 and 8 free, and leave 1 at a center that does not exist. In the second example, one center pays 6 to get 5 and 4 free, and the other pays 3 to get 2 and 1 free.
Editorial
Loading solution…