Keep Differences at Most M
Time limit3sMemory limit1024 MB
Find the fewest elements to change so every adjacent pair differs by at most M, where each changed value can be any integer.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Math, Array
- Solved
- No attempts yet
Problem
Dongwoo is analyzing a sequence of length . In one operation, Dongwoo can change one element of the sequence to any integer he wants. Dongwoo does not like tedious work, so he wants every pair of adjacent elements to differ by at most using as few operations as possible. In other words, the goal is for all . Help Dongwoo find the minimum number of operations required.
Input
The first line contains two integers () and (), separated by a space. The second line contains integers (), separated by spaces.
Output
On the first line, print the minimum number of operations needed to make every pair of adjacent elements differ by at most .
If it is impossible, print -1.