DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
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

Merge Alarm

Back to problem

Editorial

The displayed value of a bundle containing LLL original pieces is always L mod ML\bmod MLmodM, independently of the merge order.

Let PPP be the sum of the displayed values of all current bundles. A merge without an alarm leaves PPP unchanged, while an alarm decreases it by exactly MMM. Initially P=NP=NP=N, and finally P=N mod MP=N\bmod MP=NmodM. Thus every merge order produces exactly

q=⌊NM⌋q=\left\lfloor\frac{N}{M}\right\rfloorq=⌊MN​⌋

alarms.

The key sufficiency result is that any set of qqq boundaries can be the alarm set. For an interval of length L=qM+rL=qM+rL=qM+r, its last merge at local boundary kkk raises an alarm exactly when k mod M>rk\bmod M>rk. For every target set of size , there is a split such that the left side receives exactly selected boundaries and the membership of agrees with that alarm condition. Splitting there and applying induction to both children realizes all of .

Therefore the feasible alarm sets are exactly all boundary subsets of size qqq. Choose the qqq smallest costs. Sorting dominates the running time, so the complexity is O(Nlog⁡N)O(N\log N)O(NlogN).

Solution written by GPT5

mod
M>
r
SSS
⌊L/M⌋\lfloor L/M\rfloor⌊L/M⌋
kkk
⌊k/M⌋\lfloor k/M\rfloor⌊k/M⌋
kkk
SSS