Loading...
Loading...
You are given two lists of integers demands and unitCosts, and two integers capacity and penalty.
The total demand is the sum of demands, measured in units. There is one supply source for each element of unitCosts. Source i can provide any number of units from 0 to capacity, and each unit it provides costs unitCosts[i]. Every unit of demand that no source provides costs penalty instead.
Return the minimum possible total cost.
Input: demands = [5,7,3], unitCosts = [10,4,8,20], capacity = 6, penalty = 9
Output: 99
Explanation: The total demand is 5 + 7 + 3 = 15 units. Take 6 units from the source costing 4
(cost 24) and 6 units from the source costing 8 (cost 48). The remaining 3 units are not
supplied and cost 3 * 9 = 27. The sources costing 10 and 20 charge more per unit than the
penalty, so using them would only raise the total. The total is 24 + 48 + 27 = 99.
Input: demands = [4,2], unitCosts = [3,1,2], capacity = 5, penalty = 10
Output: 7
Explanation: The total demand is 6 units. Take 5 units from the source costing 1 (cost 5) and
1 unit from the source costing 2 (cost 2). Every unit is supplied, so no penalty is paid.
The total is 5 + 2 = 7.
Input: demands = [2,3], unitCosts = [7,9], capacity = 10, penalty = 5
Output: 25
Explanation: Both sources charge more per unit than the penalty, so it's cheapest to leave all
5 units unsupplied. The total is 5 * 5 = 25.
demands.length ≤105demands[i] ≤104unitCosts.length ≤105unitCosts[i] ≤104capacity ≤104penalty ≤104