Loading...
Loading...
For the college fest, the decoration team planted n flowers in a row along the corridor. Flower i blooms on day b[i] and stays open after that.
A bouquet needs k flowers that are next to each other in the row and have all bloomed. Each flower can go into only one bouquet. The team needs m bouquets.
Print the smallest day on which m bouquets can be made. If it is never possible, print -1.
-1.Example 1
Input
5 3 1 1 10 3 10 2
Output
3
On day 3, flowers 1, 3 and 5 are open: three bouquets of one flower each.
Example 2
Input
5 3 2 1 10 3 10 2
Output
-1
Three bouquets of 2 need 6 flowers, but there are only 5.
Try the problem first. Open one hint at a time.
If m bouquets are possible on some day, they are also possible on every later day.
So test a day d directly: walk along the row, count open flowers in a row, and make a bouquet each time the run reaches k.
Search for the smallest good day between the smallest and the largest bloom day by halving the range.
First rule out the impossible case: if m × k is more than n, print -1. Compute m × k as a long.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor