Increasing Sequence

Practice this coding question on Coding Gurukul.

Problem statement

A workshop's calibration stations must be visited in order. Each station has a recorded setting, but some settings are too small to follow the previous station. The technician can raise any one setting by exactly d units per adjustment and wants to use as few adjustments as possible. Your task Make the sequence strictly increasing by repeatedly choosing one element and adding d to it. Report the minimum total number of adjustments. The order of the stations cannot change, and values cannot be decreased. Important details Strictly increasing means every value is greater than its immediate predecessor; equality is not enough. A station may be adjusted several times. The final settings may exceed the input bound. Use a 64-bit integer for the total number of operations. Worked example For settings 5 3 3 8 and d = 3, raise the second setting once to 6, the third twice to 9, and the fourth once to 11. The resulting sequence is 5 6 9 11, using four adjustments. An already increasing sequence needs zero adjustments.

Input format

First line: n d. Second line: n integer settings.

Output format

Print the minimum number of adjustments.

Constraints

2 <= n <= 2000; 1 <= d <= 1000000; 1 <= setting[i] <= 1000000.

Sample input

4 3
5 3 3 8

Sample output

4

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…