Under construction

Still being built. Things may change

Even Electricity

Time limit1sMemory limit512 MB

Summary
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 nn coming days. On the ii'th day, the solar power station generates sis_i units of electricity. At the same time, wiw_i 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 rr units of water, so at the end of each day the amount of water in the reservoir cannot be larger than rr. 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 nn days must at some point be converted to electricity.

Let eie_i be the amount of electricity produced on the ii'th day. Note that ei=si+rie_i = s_i + r_i, where rir_i is how much water from the reservoir you converted on the ii'th day. Your task is to find the minimum possible value of max⁡i(ei)−min⁡i(ei)\max_i(e_i) - \min_i(e_i).

Input

The first line contains two integers nn and rr (1≤n≤1051 \leq n \leq 10^5, 1≤r≤1091 \leq r \leq 10^9), the number of days and the amount of water the reservoir can hold. The following nn lines each contain two integers sis_i and wiw_i (0≤si,wi≤1090 \leq s_i, w_i \leq 10^9), the amount of electricity generated by the solar power station and the amount of water that flowed into the reservoir on the ii'th day.

Output

Output the minimum possible value of max⁡i(ei)−min⁡i(ei)\max_i(e_i) - \min_i(e_i).

Examples1

  1. Example 1

    Input
    3 1
    4 2
    2 0
    5 1
    
    Expected output
    3