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

Ddudda and Alarm Clocks

Back to problem

Editorial

Let YYY be the OR value of a chosen non-empty set. We want the maximum possible YYY such that Y≤MY \le MY≤M.

Consider integer comparison in binary. For Y≤MY \le MY≤M, one of the following must hold.

  • Y=MY = MY=M.
  • At the highest bit bbb where YYY and MMM differ, MMM has 11 and has , while all higher bits are equal.

In the second case, lower bits below bbb can be arbitrary. Therefore, every value Y≤MY \le MY≤M is a bitwise submask of one of the following masks.

  • MMM itself.
  • A value obtained by changing one set bit bbb of MMM to 000 and changing all lower bits to 111.

Call such a candidate mask BBB. For the final OR value to be a submask of BBB, every chosen AiA_iAi​ must also be a submask of BBB. This is equivalent to

(AiOR⁡B)=B(A_i \operatorname{OR} B) = B(Ai​ORB)=B

For a fixed BBB, if we OR all values satisfying this condition, the result is still a submask of BBB, so it is at most BBB and therefore at most MMM. Also, bitwise OR never decreases when more values are chosen, so choosing all usable values is optimal for that BBB.

Thus, for every candidate mask BBB, compute

⋁ (AiOR⁡B)=BAi\bigvee_{\,(A_i \operatorname{OR} B) = B} A_i(Ai​ORB)=B⋁​A

The answer is the maximum among candidates where at least one value was usable. If no candidate allows any value, print −1-1−1.

There are at most 606060 candidate masks, and for each one we scan all NNN values. The time complexity is O(60N)O(60N)O(60N). Apart from the input array, the extra memory usage is O(60)O(60)O(60).

1
YYY
000
i
​