Icicles

Time limit1sMemory limit128 MB

Summary
Icicles grow each hour when strictly longer than both neighbors and snap at length L; find the hour when all have broken.
Level

Hard8 of 10

Topics
Simulation, Implementation, Array, Math
Solved
No attempts yet

Problem

Under the eaves of JOI's house in Canada, a fine row of icicles has formed. Curious about them, JOI decides to take a closer look.

There are NN icicles (2≤N≤1000002 \le N \le 100000) hanging in a straight line under the eaves. The ii-th icicle (1≤i≤N1 \le i \le N) hangs at the position ii cm from the left end, and its initial length is aia_i cm (where aia_i is a positive integer). The icicles grow according to the following rules.

  • The ii-th icicle grows by 11 cm each hour if and only if it is strictly longer than both the (i−1)(i-1)-th and the (i+1)(i+1)-th icicles. For the two icicles at the ends, only the single existing neighbor is considered: the 11st icicle grows while it is longer than the 22nd, and the NN-th icicle grows while it is longer than the (N−1)(N-1)-th.
  • The moment an icicle reaches a length of LL cm (2≤L≤500002 \le L \le 50000), it snaps off at the base. A broken icicle is thereafter treated as an icicle of length 00 cm.

Initially, every two adjacent icicles have different lengths. Under this condition, after enough time all NN icicles will have snapped off and become 00 cm long. Compute the number of hours it takes until every icicle has broken.

Input

The first line contains two integers NN and LL, the number of icicles and the breaking length, separated by a space. Each of the next NN lines contains one integer aia_i (1≤ai<L1 \le a_i < L), the initial length of the ii-th icicle.

Output

Print a single integer: the number of hours until all icicles have broken.

Hint

Consider icicles with initial lengths 4,2,3,54, 2, 3, 5 and breaking length L=6L = 6. The 11st, 22nd, 33rd, and 44th icicles break after 22, 88, 44, and 11 hours respectively. Hence all icicles have broken after 88 hours, so the answer is 88.

Examples2

  1. Example 1

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

    Input
    6 10
    3
    4
    1
    9
    5
    1
    
    Expected output
    15