Air Currents
Time limit2sMemory limit1024 MB
Raise one air current by at most m to minimize the sum of i minus the index of the nearest strictly greater element to its left.
- Level
Hard8 of 10
- Topics
- Stack, Segment tree, Greedy
- Solved
- No attempts yet
Statement
Kolobok left his grandmother and set off to travel. By accident he wandered into the land of Ivenland. His first trouble was a huge moat of water in front of the entrance, and that water is known to be bad for our hero. Fortunately, air currents are placed all around, and each one lifts whoever stands on it to a certain height. Since the country is called Ivenland, every height an air current can lift the hero to is an even number.
Let the air currents be an array of positive integers, the heights of the currents. For each , let be the index of the nearest element to the left of that is strictly greater than . Formally, . If or no element to the left of is greater than , then .
Kolobok measures the layout by . The smaller the sum, the better the layout. The only thing Kolobok can do is raise the height of one air current by at most . The height must remain an integer after the change, but it may become odd if needed.
Help Kolobok make the change that makes as small as possible.
Input
The first line contains and (, ), the number of air currents and the maximum amount by which one of them can be raised. The second line contains the heights (). All heights are guaranteed to be even.
Output
Print one integer, the minimum possible value of the sum.
Hint
In the first example, raising the first element by 3 to make it 7 is beneficial. Then , , , and , so . No change can give .
In the second example, , so the first element cannot be raised that much. After any action, is 5 or 6. Raising the first element by 1 gives , where .