P28146 [洛谷] 『FLA - I』Cloud Melody
提高-
题目描述

Problem Description

In a dream, Qiu planted $n$ withered roses. He remembers that the $i$-th rose was planted at time $t_i$.

The withered roses bloom immediately upon being planted, but each rose only blooms for $m$ moments (a rose planted at time $T$ will bloom only from time $T$ to time $T+m-1$). After $m$ moments, they turn into dust that can no longer be retained, scattering in the biting cold wind.

He asks you, if he can change the planting time of no more than one rose (select a $t_i$ and modify it to any positive integer), how many moments can have exactly one withered rose blooming at most?

Input Format

The first line contains two positive integers $n$ and $m$.

The second line contains $n$ positive integers, where the $i$-th integer is $t_i$.

Output Format

Output a single positive integer representing the answer.

Sample Input and Output

Sample Input #1

5 4
11 9 1 3 12

Sample Output #1

14

Sample Input #2

13 7
6 42 58 41 20 60 2 61 45 28 45 28 12

Sample Output #2

38

Notes

「Sample Explanation #1」

As shown in the figure, moments with exactly one withered rose blooming are marked in gold, and the blooming moments of each rose are marked in black and red.

example1

After changing the planting time of the rose marked in red to $17$ (modifying the value of $t_1$ to $17$, as shown below), there are $14$ moments with exactly one withered rose blooming. It can be proven that there is no modification scheme that can make more than $14$ moments have exactly one withered rose blooming.

example2

「Data Range」

Test Point $n \leq$ $m \leq$ $t_i \leq$
$1 \sim 6$ $5000$ $5000$ $5000$
$7 \sim 12$ $2 \times 10^5$ $2 \times 10^5$ $2 \times 10^5$
$13 \sim 14$ $2 \times 10^5$ $1$ $10^9$
$15 \sim 20$ $2 \times 10^5$ $10^9$ $10^9$

For all test data, $1 \leq n \leq 2 \times 10^5$, $1 \leq m, t_i \leq 10^9$.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
5 4 11 9 1 3 12
样例输出
14