Landscaping
Time limit2sMemory limit512 MB
Move, buy, or remove dirt so each of N flowerbeds reaches its target amount at minimum total cost.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Graph
- Solved
- No attempts yet
Problem
Farmer John is rebuilding his garden, and the work involves moving a large amount of dirt.
The garden is a row of flowerbeds (). Flowerbed starts with units of dirt. Farmer John wants flowerbed to hold exactly units of dirt once the work is done. Every and every is an integer between and .
Farmer John has three moves.
- Buy one unit of dirt and place it in any flowerbed, at a cost of .
- Remove one unit of dirt from any flowerbed and have it shipped away, at a cost of .
- Transport one unit of dirt from flowerbed to flowerbed , at a cost of .
Compute the minimum total cost of bringing every flowerbed to its target state.
Input
The first line contains , , , and (, ). Line contains the integers and .
Output
Print the minimum total cost of the landscaping work.
Note
The same task appeared earlier with much smaller limits. This version raises them considerably, so a solution written for the easier version will not finish in time.