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

Number Partition

Back to problem

Editorial

Consider a slightly generalized problem: divide the numbers in [1,m][1,m][1,m] into three piles with sums s1,s2,s3s_1,s_2,s_3s1​,s2​,s3​, where some sis_isi​ may be 000.

For m≥3m \geq 3m≥3, the only impossible cases are the following:

  • At least two of the sis_isi​ are equal to 111.
  • At least two of the sis_isi​ are equal to 22.

A pile with sum 111 must be exactly {1}\{1\}{1}, and a pile with sum 222 must be exactly {2}\{2\}{2}, so the cases above are impossible.

We construct the partition by placing the numbers from large to small. Suppose we are currently processing m,m−1,⋯ ,1m,m-1,\cdots,1m,m−1,⋯,1. If the remaining target sums are s1,s2,s3s_1,s_2,s_3s1​,s, placing into pile changes the remaining sum of that pile to . Try all piles where this is nonnegative, and choose one that keeps the remaining instance on feasible by the condition above.

Starting from a feasible state, such a choice always exists. Therefore, repeating this for m=n,n−1,⋯ ,1m=n,n-1,\cdots,1m=n,n−1,⋯,1 gives a valid partition.

If the initial state is impossible, print NO\text{NO}NO. Otherwise, output the three piles obtained by the construction.

Solution written by GPT5.5

2
2​
,
s3​
mmm
iii
si−ms_i-msi​−m
[1,m−1][1,m-1][1,m−1]