Even Electricity
Time limit1sMemory limit512 MB
Choose how much stored water to convert each day so that daily electricity output has the smallest possible max minus min.
- Level
Hard8 of 10
- Topics
- Greedy, Two pointers, Prefix sum
- Solved
- No attempts yet
Problem
A town gets all its electricity from solar power and a dam that provides hydroelectric power. These sources of energy are not always the most reliable, since the amount of sun and water can vary a lot from day to day. Luckily, the dam has a large reservoir that can store water so that it can even out the supply of electricity from day to day.
Problem
You will be given information about the coming days. On the 'th day, the solar power station generates units of electricity. At the same time, units of water will flow into the reservoir. Each unit of water can be converted into one unit of electricity, and you can decide how much to convert and how much to leave in the reservoir. However, the reservoir can only hold units of water, so at the end of each day the amount of water in the reservoir cannot be larger than . At the beginning of the first day the reservoir is empty.
On the first day, there is no water stored in the reservoir. At the end of the last day, the dam will undergo maintenance and must be empty. In other words, all the water that flows into the dam during the days must at some point be converted to electricity.
Let be the amount of electricity produced on the 'th day. Note that , where is how much water from the reservoir you converted on the 'th day. Your task is to find the minimum possible value of .
Input
The first line contains two integers and (, ), the number of days and the amount of water the reservoir can hold. The following lines each contain two integers and (), the amount of electricity generated by the solar power station and the amount of water that flowed into the reservoir on the 'th day.
Output
Output the minimum possible value of .