#P10837. Maximizing Unique Bloom Moments

    ID: 12879 Type: Default 1000ms 256MiB

Maximizing Unique Bloom Moments

Maximizing Unique Bloom Moments

In a dream, Qiu planted \( n \) wilting roses. He remembers that the \( i\text{-}th \) rose was planted at time \( t_i \). Each rose blooms immediately when planted and continues to bloom for exactly \( m \) consecutive time instants, i.e. a rose planted at time \( T \) will bloom at times \( T, T+1, \ldots, T+m-1 \). After that, it withers into dust.

You are allowed to change the planting time of at most one rose (i.e. choose one \( t_i \) and change it to any positive integer). Determine the maximum number of time instants at which exactly one rose is blooming.

Note: The timeline is unbounded (positive integers) and the blooming intervals of the unmodified roses are fixed. When you change the planting time of a rose, you lose the time instants that that rose originally contributed (if they were unique) but you can replant it in a gap that does not intersect with any other rose’s blooming intervals, thereby contributing \( m \) new instants. Formally, if the original configuration has a total of \( B \) instants with exactly one rose blooming, and if the rose you modify originally contributed \( c_i \) such instants, then after replanting optimally the total unique instants can be \( B - c_i + m \). You must choose the best option among not modifying any rose and modifying any one rose.

Input constraints: \( 1 \le n \le 2\cdot10^5 \), \( 1 \le m \le 10^9 \), and \( 1 \le t_i \le 10^9 \). It is guaranteed that the input size is large so use fast input/output methods.

inputFormat

The first line of input contains two integers \( n \) and \( m \) — the number of roses and the blooming duration respectively.

The second line contains \( n \) integers \( t_1,t_2,\ldots,t_n \) — the times at which each rose was planted.

outputFormat

Output a single integer — the maximum number of time instants at which exactly one rose is blooming after changing the planting time of at most one rose.

sample

3 5
3 5 8
10

</p>