Under construction

Still being built. Things may change

Air Currents

Time limit2sMemory limit1024 MB

Summary
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 h[1..n]h[1..n] of nn positive integers, the heights of the currents. For each 1≤i≤n1 \le i \le n, let G[i]G[i] be the index of the nearest element to the left of ii that is strictly greater than h[i]h[i]. Formally, G[i]=max⁡{j∣j<i and h[j]>h[i]}G[i] = \max\{j \mid j < i \text{ and } h[j] > h[i]\}. If i=1i = 1 or no element to the left of ii is greater than h[i]h[i], then G[i]=0G[i] = 0.

Kolobok measures the layout by S(h)=∑i=1n(i−G[i])S(h) = \sum_{i=1}^n (i - G[i]). 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 mm. The height must remain an integer after the change, but it may become odd if needed.

Help Kolobok make the change that makes S(h)S(h) as small as possible.

Input

The first line contains nn and mm (1≤n≤1051 \le n \le 10^5, 1≤m≤1091 \le m \le 10^9), the number of air currents and the maximum amount by which one of them can be raised. The second line contains the heights h[i]h[i] (1≤h[i]≤1091 \le h[i] \le 10^9). 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 h=[7,2,6]h = [7, 2, 6], G[1]=0G[1] = 0, G[2]=1G[2] = 1, and G[3]=1G[3] = 1, so S(h)=(1−0)+(2−1)+(3−1)=1+1+2=4S(h) = (1 - 0) + (2 - 1) + (3 - 1) = 1 + 1 + 2 = 4. No change can give S(h)<4S(h) < 4.

In the second example, m=2m = 2, so the first element cannot be raised that much. After any action, S(h)S(h) is 5 or 6. Raising the first element by 1 gives [5,2,6][5, 2, 6], where S(h)=5S(h) = 5.

Examples3

  1. Example 1

    Input
    3 100
    4 2 6
    
    Expected output
    4
    
  2. Example 2

    Input
    3 2
    4 2 6
    
    Expected output
    5
    
  3. Example 3

    Input
    3 10
    2 2 2
    
    Expected output
    4