Joe is a biomedical researcher. He is close to a cure for a terrible disease. Making the drug requires a special enzyme that is expensive and loses its properties after a fixed time. The clinical trial phase needs one dose of the drug every hour, and one dose takes one enzyme.
Prices are given for the next n hours. At hour i Joe can buy any number of enzymes at price ci each. An enzyme lives for h hours, so an enzyme bought at hour i can be used at hours i through i+h−1. Joe uses one enzyme at each of the hours 1 through n. Find the plan with the smallest total cost.
When prices tie, Joe buys the fresher enzyme instead of stocking one early. That fixes a single plan: the enzyme used at hour i is bought at the cheapest hour of the interval [max(1, i−h+1), i], and if several hours are cheapest, at the latest of them.