QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 512 MB Total points: 100

#4897. Music Master

Statistics

You are playing a rhythm game. In this level, there are $n$ notes falling from the top of the screen, and you need to hit them on a line at the bottom of the screen.

The judgment line can be viewed as the positive half-axis of a number line. The $n$ notes fall in sequence, with the $i$-th note falling at position $p_i$. Your two hands can each cover an interval of length $L$. Specifically, a hand can cover $[x, x+L]$, allowing you to hit any note that falls within this interval (inclusive of the endpoints).

At the start of the game, your two hands cover $[0, L]$ and $[0, L]$. In the intervals between the falling of the $n$ notes, you can move your hands arbitrarily. The time taken for movement is negligible, and moving a hand from $[x, x+L]$ to $[y, y+L]$ consumes $|x-y|$ stamina.

Before the game starts, you want to know the minimum total stamina required to hit all the notes in sequence.

Input

The first line contains two integers $n$ and $L$.

The second line contains $n$ integers, where the $i$-th integer $p_i$ represents the position of the $i$-th note.

Output

A single integer representing the minimum stamina consumed.

Examples

Input 1

4 1 
6 3 7 1

Output 1

9

Note

The movements of the two hands are: $[0,1] \to [5,6] \to [6,7]$ and $[0,1] \to [2,3] \to [1,2]$, consuming $6+3=9$ stamina.

Input 2/3

See the provided files ex_game2/3.in.

Output 2/3

See the provided files ex_game2/3.out.

Constraints

For $100\%$ of the data, $1 \le n \le 50000$, $0 \le L \le 50$, $0 \le p_i \le 10^9$.

Please note that the third sample test case (Extra Test #3) does not satisfy the $L \le 50$ constraint; its $L$ value is 68.

$subtask1(15\%): n \le 200, p_i \le 200$

$subtask2(15\%): L = 0$

$subtask3(30\%): L \le 5$

$subtask4(40\%):$ No special constraints.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.