DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Defense

Back to problem

Editorial

Let

C=⌈HK⌉C=\left\lceil \frac{H}{K} \right\rceilC=⌈KH​⌉

be the number of attacks required to defeat one monster.

A monster can be attacked at most NNN times from the moment it is spawned until the moment it reaches the castle. Therefore, if C>NC>NC>N, no monster can be defeated, and the answer is 000.

Now assume C≤NC \le NC≤N. 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 N+M−1N+M-1N+M−1 seconds in total. Since defeating one monster requires CCC attacks, the number of monsters that can be defeated is at most

⌊N+M−1C⌋.\left\lfloor \frac{N+M-1}{C} \right\rfloor.⌊CN+M−1​⌋.

Of course, there are only MMM monsters in total. Therefore, the answer is

min⁡(M,⌊N+M−1C⌋).\min\left(M,\left\lfloor \frac{N+M-1}{C} \right\rfloor\right).min(M,⌊CN+M−1​⌋).

The time complexity is O(1)O(1)O(1) per test case.