Dao's Race Competition
InterviewTime limit1sMemory limit1024 MB
Find the fewest tracks to extend by K (each at most once) so the lengths strictly increase, or report -1 if impossible.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem

Dao is practicing for a race.
Dao prepared tracks for a race meet. The track raced -th has length .
The audience wants the race to be exciting, which means the track lengths should keep getting longer. In other words, the race is exciting when . To do this, Dao can perform the following operation zero or more times.
- Choose an index with , then increase the length of the -th track by . That is, change to .
- For each , this operation can be performed at most once.
Determine whether Dao can make the track lengths strictly increasing. If possible, find the minimum number of operations needed.
Input
The first line contains the number of tracks and the length by which a track can be extended, separated by a space.
The second line contains the integers , , , , which give the length of each track, separated by spaces.
Output
If Dao can make the track lengths strictly increasing, print the minimum number of operations needed.
If Dao cannot make the track lengths strictly increasing, print -1.