Given an array of positive integers, replace each element in the array such that the difference between adjacent elements in the array is less than or equal to a given target. We need to minimize the adjustment cost, that is the sum of differences between new and old values. We basically need to minimize ∑|A[i] – A_{new}[i]| where 0 <= i <= n-1, n is size of A[] and A_{new}[] is the array with adjacent difference less that or equal to target.

**Input:**

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains two integers N and K where N denotes the size of array A and K is the target.

The second line of each test case contains N space separated integers denoting elements of the array A[ ].

**Output:**

Print the minimum adjustment cost for each test case in a new line.

**Constraints:**

1<= T <=100

1<= N, K <=1000

1<= A[ ] <=100

**Example:**

**Input:**

4 1

1 3 0 3

**Output:**

3

Author: sujnesh

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

parass321 | 46 |

YogeshSharma7479 | 46 |

ayushwagdre1 | 44 |

yashranjan74 | 42 |

geekscoder | 41 |

Pulkit__Sharma__ | 806 |

Anirban166 | 682 |

Found_me | 636 |

abducodes | 607 |

Sathvik Avasarala | 526 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4559 |

Login to report an issue on this page.