Yogurt factory
InterviewTime limit1sMemory limit1024 MB
Given weekly production costs, delivery quantities, and a per-week storage fee, find the minimum total cost to meet every week's delivery.
- Level
Medium5 of 10
- Topics
- Greedy, Dynamic programming
- Solved
- No attempts yet
Problem
The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next () weeks, the price of milk and labor fluctuates weekly, so producing one unit of yogurt in week costs the company cents (). The factory is well designed and can produce arbitrarily many units of yogurt each week.
Yucky Yogurt owns a warehouse that stores unused yogurt at a constant fee of () cents per unit per week. Yogurt does not spoil. The warehouse is enormous and can hold arbitrarily many units.
Yucky wants to make weekly deliveries of () units of yogurt to its clients, where is the delivery quantity in week . Find a schedule that minimizes the total cost over the weeks. Yogurt produced in week , as well as yogurt already in storage, can be used to meet the demand for that week.
Input
The first line contains two space-separated integers and .
Line (for ) contains two space-separated integers and .
Output
The first line contains a single integer, the minimum total cost to satisfy the yogurt schedule. The total may not fit in a 32-bit integer.
Hint
In week 1, produce 200 units of yogurt and deliver all of it. In week 2, produce 700 units: deliver 400 units while storing 300 units. In week 3, deliver the 300 units that were stored. In week 4, produce and deliver 500 units.