A vineyard's aging cellar holds n numbered barrel racks arranged in a fixed row. Rack i currently holds current[i] liters of wine, but the outbound shipment manifest requires rack i to hold exactly target[i] liters once loading begins. You have two tools available, and you may use them in any combination:
Given the current volumes, the target volumes, and the flat fee, find the minimum total money needed so that, once you are done, rack i holds exactly target[i] liters for every i from 0 to n - 1.
Print a single integer: the minimum total cost.
Example 1
Input
3 3 1 10 1 10 1 1
Expected
3
Explanation
Direct adjustment at every rack costs |1-10|+|10-1|+|1-1| = 9+9+0 = 18. Instead, call a relabeling day for the flat fee of 3: sorting the current volumes gives [1,1,10] and sorting the target volumes also gives [1,1,10], a perfect match, so no further adjustment is needed after the crew rearranges the wine. Total cost is just the fee, 3, which beats 18.
Example 2
Input
3 100 5 5 5 5 5 6
Expected
1
Explanation
Direct adjustment costs |5-5|+|5-5|+|5-6| = 0+0+1 = 1. A relabeling day would cost the flat fee of 100 plus at least 1 more (sorted current [5,5,5] and sorted target [5,5,6] still differ by 1), far more expensive. The cheapest plan is the direct adjustment, costing 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →