DOJ logoDOJ
LoginRegister
DOJ logoDOJ
Call for Tasks
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

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
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Editorial

Pepero Packing

Back to problem

Editorial

Let Fi=(Bi,Ui)F_i=(B_i,U_i)Fi​=(Bi​,Ui​) be the optimal state using the first iii sticks: maximize boxes BiB_iBi​, then minimize used sticks UiU_iUi​. Leaving stick iii unused gives Fi−1F_{i-1}Fi−1​. If the last box is the segment k+1,…,ik+1,\ldots,ik+1,…,i, it requires prefix sums Qi−Qk≥XQ_i-Q_k\geq XQi​−Qk​≥X and gives (Bk+1,Uk+i−k)(B_k+1,U_k+i-k)(Bk​+1,Uk​+i−k). For a fixed last box the preceding prefix must be optimal, so this recurrence covers every solution. Subtask 1 checks every kkk in O(N2)O(N^2)O(N2) time. For the full task, positive weights make prefix sums increasing. The indices satisfying Qk≤Qi−XQ_k\leq Q_i-XQk​≤Qi​−X become eligible in order; a pointer adds each once. Keep the eligible state maximizing BkB_kBk​ and, among ties, minimizing Uk−kU_k-kUk​−k. Compare its candidate with Fi−1F_{i-1}Fi−1​ at every position. Time is O(N)O(N)O(N) and space is O(N)O(N)O(N).

Solution written by GPT6.1