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 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
Editorial

Pepero Disposal

Back to problem

Editorial

Let sis_isi​ be 1 if person iii initially faces right and 0 otherwise. Define the final-direction indicator cic_ici​ similarly. Let li,ril_i,r_ili​,ri​ be their total left and right departures. Alternation implies ri−li=si−cir_i-l_i=s_i-c_iri​−li​=si​.

Put Pi=∑j≤iAjP_i=\sum_{j\leq i}A_jPi​=∑j≤i​Aj​ and let be the number of sticks discarded to the left. Conservation across each edge gives , with and . Consequently,

W=∑i=1NPi−∑i=1Nsi,(N+1)L=W+∑i=1Nci.W=\sum_{i=1}^{N}P_i-\sum_{i=1}^{N}s_i,\qquad (N+1)L=W+\sum_{i=1}^{N}c_i.W=i=1∑N​P

Since 0≤∑ci≤N0\leq\sum c_i\leq N0≤∑ci​≤N, both L=⌈W/(N+1)⌉L=\lceil W/(N+1)\rceilL=⌈W/(N and the final number of right-facing people are determined.

Scan from left to right, maintaining lil_ili​ and the remaining number KKK of right-facing final directions. Set ci=1c_i=1ci​=1 precisely when , , and , and decrement . Otherwise set . Update and . The answer is .

To justify the greedy choice, put Yi=∑j≤icjY_i=\sum_{j\leq i}c_jYi​=∑j≤i​cj​, , and . Nonnegative departures are equivalent to

Yi≤Ui=iL−Qi−1+Hi+min⁡(0,L−Pi).Y_i\leq U_i=iL-Q_{i-1}+H_i+\min(0,L-P_i).Yi​≤Ui​=iL

Because PiP_iPi​ is nondecreasing, UiU_iUi​ first increases and then decreases, ending at the required total number of final right directions. Thus taking a feasible 1 as early as possible, while respecting this total, never violates a later upper bound. Comparing prefixes against any feasible final state shows that greedy never chooses its 1s later, so it reaches the required total. All departures are nonnegative and the boundary conditions hold.

It remains to show these counts describe a legal execution. A fired person's last departure is opposite their final direction. On a path, a directed last-departure cycle could only be two neighbors whose final directions are left, right. If greedy creates this transition, ri=0r_i=0ri​=0 or li+1=0l_{i+1}=0li+1​, implying one endpoint never fires. Hence there is no last-departure cycle among fired people.

Any legal execution cannot exceed the computed quota at a vertex: at the first excess firing, neighbors have sent at most their quotas, so conservation leaves no stick there. If an execution finished with unused quotas, the remaining departures would be balanced at each vertex and send no stick to a sink. Following their last departures would create a directed cycle, a contradiction. Therefore every legal execution has exactly these counts, and their sum is the minimum.

The time and space complexities are O(N)O(N)O(N). Use signed 64-bit integers. For C=∑AiC=\sum A_iC=∑Ai​, we have li≤iC+i−1l_i\leq iC+i-1l, giving the safe answer bound .

Solution written by GPT6.1

−
ci​
LLL
ri−li+1=Pi−Lr_i-l_{i+1}=P_i-Lri​−li+1​=Pi​−L
l1=Ll_1=Ll1​=L
lN+1=0l_{N+1}=0lN+1​=0
i​
−
i=1∑N​si​,(N+
1)L=
W+
i=1∑N​ci​.
+
1)⌉
K=(N+1)L−WK=(N+1)L-WK=(N+1)L−W
K>0K>0K>0
li+si≥1l_i+s_i\geq1li​+si​≥1
li+si−(Pi−L)≥1l_i+s_i-(P_i-L)\geq1li​+si​−(Pi​−L)≥1
KKK
ci=0c_i=0ci​=0
ri=li+si−cir_i=l_i+s_i-c_iri​=li​+si​−ci​
li+1=ri−(Pi−L)l_{i+1}=r_i-(P_i-L)li+1​=ri​−(Pi​−L)
∑(li+ri)\sum(l_i+r_i)∑(li​+ri​)
Hi=∑j≤isjH_i=\sum_{j\leq i}s_jHi​=∑j≤i​sj​
Qi=∑j≤iPjQ_i=\sum_{j\leq i}P_jQi​=∑j≤i​Pj​
−
Qi−1​+
Hi​+
min(0,L−
Pi​).
=
0
i​
≤
iC+
i−
1
CN(N+1)+N2≤4 000 020 040 000 000 000CN(N+1)+N^2\leq4\,000\,020\,040\,000\,000\,000CN(N+1)+N2≤4000020040000000000