#P3202. Rescuing the Princess: Minimizing Coin Expenditure

    ID: 16459 Type: Default 1000ms 256MiB

Rescuing the Princess: Minimizing Coin Expenditure

Rescuing the Princess: Minimizing Coin Expenditure

Peng Daxia has resolved to rescue the princess who is imprisoned in a castle. However, the road to the castle is lined with supports located at positions (x, hx) for x = 1 to n, lying in the first quadrant. Peng Daxia starts at the first support (1, h1) and the castle entrance is at (n, hn).

Peng Daxia can only jump from one support to the next, i.e. from support (x, hx) to support (x+1, hx+1). However, his jump has limited energy: he cannot overcome a vertical difference greater than a given value d. Formally, a jump from support i to support i+1 is only possible if

\(|h_{i+1} - h_{i}| \le d\)

At the starting point, Peng Daxia possesses a special ability: by spending 1 coin he can either increase or decrease the height of any support by 1 unit. This operation can be applied to any support except for the starting support (support 1) and the destination support (support n). However, once he leaves the start, he cannot adjust the supports further.

The goal is to modify the heights of the intermediate supports (if necessary) to ensure that every jump from support 1 to support n is feasible under the energy constraint. Since 100 coins can be exchanged for 1 unit of life, Peng Daxia wants to minimize the number of coins spent.

Your task is to compute the minimum total number of coins required so that Peng Daxia can reach the castle.

inputFormat

The first line contains two integers n and d, where 2 ≤ n ≤ 100 indicates the number of supports, and d (0 ≤ d ≤ 100) is the maximum vertical difference Peng Daxia can overcome in a single jump.

The second line contains n positive integers representing the heights of the supports: h1, h2, ..., hn. The heights of the starting support (h1) and the castle entrance (hn) cannot be changed.

outputFormat

Output a single integer --- the minimum number of coins needed to adjust the intermediate supports so that Peng Daxia can reach the castle by jumping between consecutive supports under the jump constraint.

sample

3 1
5 3 5
1

</p>