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

Round-Robin Tree

Back to problem

Editorial

Let Fv(k)F_v(k)Fv​(k) be the maximum score obtainable by sending kkk balls into the subtree rooted at vertex vvv.

First handle vertices with exactly one child. If the only child of vvv is uuu, every ball must move to uuu, so

Fv(k)=Fu(k).F_v(k)=F_u(k).Fv​(k)=Fu​(k).

Therefore, contracting every vertex with exactly one child does not change any distribution or score. A unary chain starting at the original root can be contracted in the same way. Every vertex in the contracted tree is now either a leaf or has at least two children.

For a leaf vvv, every ball stops there, hence

Fv(k)=kCv.F_v(k)=kC_v.Fv​(k)=kCv​.

Now consider a vertex vvv with ddd children. Write k=qd+rk=qd+rk=qd+r, where 0≤r<d0\le r<d0≤r. Independently of the chosen order, every child receives balls, and exactly the first children receive one additional ball.

For a child uuu, the gain from receiving that additional ball is

Du(q)=Fu(q+1)−Fu(q).D_u(q)=F_u(q+1)-F_u(q).Du​(q)=Fu​(q+1)

Any subset of rrr children can be placed in the first rrr positions. Therefore, the optimal choice is to select the rrr largest values of Du(q)D_u(q)Du​(q), giving

Fv(k)=∑uFu(q)+the sum of the r largest values among Du(q).F_v(k)=\sum_u F_u(q)+\text{the sum of the $r$ largest values among $D_u(q)$}.Fv​(k)=u∑​

For one fixed qqq, all states k=qd,qd+1,⋯ ,qd+d−1k=qd,qd+1,\cdots,qd+d-1k=qd,qd+1,⋯,qd+d− can be computed together. Sort all values in descending order and use their prefix sums.

Computing every state up to XXX at every vertex is still too slow. Let KvK_vKv​ be the maximum number of balls for which a state at vvv can be required. For the contracted root, Kroot=XK_{root}=XK. If has children, each child can receive at most

Ku=⌈Kvd⌉K_u=\left\lceil\frac{K_v}{d}\right\rceilKu​=⌈dKv​

balls. Thus, only Fu(0),Fu(1),⋯ ,Fu(Ku)F_u(0),F_u(1),\cdots,F_u(K_u)Fu​(0),Fu​(1),⋯,F are needed.

To bound the number of states, define Ev=Kv−1E_v=K_v-1Ev​=Kv​−1. For every child uuu,

Eu=⌊Evd⌋.E_u=\left\lfloor\frac{E_v}{d}\right\rfloor.Eu​=⌊dEv​

Every internal vertex of the contracted tree has d≥2d\ge2d≥2, so a positive value of EEE is at least halved on every edge. Moreover, the sum of EEE over all children of one vertex does not exceed the parent's EEE. Hence, the sum of EvE_vE over one depth is at most , and positive values can occur on only depths. Vertices with contribute only a constant number of states each.

Therefore, the total number of DP states is

O(N+Xlog⁡X).O(N+X\log X).O(N+XlogX).

Sorting the marginal gains for each quotient group gives total time complexity

O((N+Xlog⁡X)log⁡N),O((N+X\log X)\log N),O((N+XlogX)logN),

and the memory complexity is

O(N+Xlog⁡X).O(N+X\log X).O(N+XlogX).

Scores may be negative, and their absolute values can reach 2⋅10142\cdot10^{14}2⋅1014, so 64-bit integers are required.

Solution written by GPT5.6

<
d
qqq
rrr
−
Fu​(q).
Fu​
(
q
)
+
the sum of the r largest values among Du​(q).
1
Du(q)D_u(q)Du​(q)
root
​
=
X
vvv
ddd
uuu
​
⌉
u​
(
Ku​
)
​
⌋
.
v
​
X−1X-1X−1
O(log⁡X)O(\log X)O(logX)
Ev=0E_v=0Ev​=0