Editorial
Let
be the number of attacks required to defeat one monster.
A monster can be attacked at most times from the moment it is spawned until the moment it reaches the castle. Therefore, if , no monster can be defeated, and the answer is .
Now assume . Instead of alternating attacks between monsters, it is never worse to keep attacking one monster until it is defeated. Also, it is never worse to first attack the closest monster among those that can still be defeated.
The total time during which attacks can be useful is from the moment the first monster is spawned until the moment the last monster reaches the castle. This is seconds in total. Since defeating one monster requires attacks, the number of monsters that can be defeated is at most
Of course, there are only monsters in total. Therefore, the answer is
The time complexity is per test case.