Time limit: 1 second Memory limit: 256 MB The math teacher does not like Amin very much…
- User
Time limit: 1 second Memory limit: 256 MB The math teacher does not like Amin very much and always forces him to solve the most difficult problems on the board.
Today, he also includes a sequel n n is a non-negative integer a 1 , a 2 , … , a n a 1 , a 2 ,...,a n wrote on the board and called Amin to the board. In one move, the teacher allows the trustee to choose one of n Erase n numbers on the board and write a number one more in its place. The teacher asks Amin to act with the least number of moves in such a way that in this sequence, consecutive numbers from 1 to h h appear somewhere.
Help Amin understand that he can achieve this goal with the fewest number of moves so that for at least one i to have i a i
1 , a i + 1
2 , … , a i + h
1
h a i =1, a i+1 =2,...,a i+h−1 =h, or indicate that this is impossible, and the teacher again teases Amin mercilessly.
input The first line of the input file contains two natural numbers n n and h h is
1 ≤ h ≤ n ≤ 200 , 000 1≤h≤n≤200,000
The second line includes n n number a i a i is --- the initial values of the elements of the written sequence. 0 ≤ a i ≤ n 0≤a i ≤n
output In the single line of the output file, write the least number of moves that the trustee needs to solve the problem, or -1 if this is impossible.
Example Sample entry 1 4 3 1 1 0 2 Plain text Sample output 1 3 Plain text Sample input 2 3 2 1 3 2 Plain text Sample output 2 -1 write code in java17
- Assistant