Closet
InterviewTime limit5sMemory limit1024 MB
Find the smallest tolerance x so that removing at most M clothes leaves a sequence that rises then falls, with each adjacent step at most x.
- Level
Medium6 of 10
- Topics
- Binary search, Dynamic programming, Array
- Solved
- No attempts yet
Problem
Taein's closet has clothes hanging in a row. The color of the -th piece from the left is .
Taein considers the closet beautiful if there is an integer () such that .
Arranging the closet into a beautiful state is quite a hassle. So Taein decided to remove at most clothes from the closet so that the remaining clothes become nearly beautiful.
After removing at most clothes, let be the number of remaining clothes and let be the color of the -th piece from the left. Taein treats two adjacent pieces as the same color if their color difference is at most . The closet is nearly beautiful if there is an integer () such that the following hold.
- for every integer with .
- for every integer with .
Find the smallest non-negative integer such that the closet can be made nearly beautiful by removing at most clothes.
Input
The first line contains two integers and . The second line contains integers , separated by spaces.
Output
Print the smallest () such that the closet can be made nearly beautiful.
Constraints
- ()
Hint
When , removing the 5th and 9th pieces from the left makes the closet nearly beautiful. No smaller can make the closet nearly beautiful.