Beautiful sequence - MarisaOJ: Marisa Online Judge

Beautiful sequence

Time limit: 1000 ms
Memory limit: 256 MB
Given a sequence of $n$ elements, denoted as $A$. Your task is to transform it into a beautiful sequence of order $d$ by performing the following steps: At each step, choose an element in the sequence and either increase or decrease that element by one unit. The sequence $b_1, b_2, ..., b_n$ is called a beautiful sequence of order $d$ if $b_i = b_{i-1} + d$ for every $i=2,3,...,n$. Find the minimum number of steps to transform $A$ into a beautiful sequence of order $d$. ### Input - The first line contains two positive integers $n, d (1 \le n \le 1000, 1 \le d \le 10^9)$. - The second line contains $n$ positive integers $A_i$. ### Output - Print the minimum number of steps to transform $A$ into a beautiful sequence of order $d$. ### Constraints - 25% of the tests have $d = 0$ and $|A_i| \le 10^3$. - 25% of the tests have $d = 0$ and $|A_i| \le 10^9$. - 25% of the tests have $d = 1$ and $|A_i| \le 10^3$. - 25% of the tests have $d \le 10^9$ and $|A_i| \le 10^9$. ### Example Input: ``` 3 1 3 2 2 ``` Output: ``` 3 ```