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

SON OF SUN

Back to problem

Editorial

Let did_idi​ be the depth of aia_iai​. Each person's energy appears once in that person's family sum and once for every ancestor. Since the total energy is zero, the desired sum is

∑i=02Ns(i)=∑i=1Ni(d2i−1−d2i).\sum_{i=0}^{2N}s(i)=\sum_{i=1}^{N}i(d_{2i-1}-d_{2i}).i=0∑2N​s(i)=i=1∑N​i(d

Exact decisions for small NNN. After processing a1,…,aja_1,\ldots,a_ja1​,…,aj​, store whether a state with maximum depth hh and weighted depth sum is reachable. The next vertex may have any depth from through . Choosing depth changes the maximum depth to and adds the next vertex's energy times to the sum. Precompute every case with .

Minimum and maximum. Define

FN(t)=2N3+3tN2+3tN−2N−5t3−6t2−t6,BN(t)=FN(t)+t(t+1).F_N(t)=\frac{2N^3+3tN^2+3tN-2N-5t^3-6t^2-t}{6},\qquad B_N(t)=F_N(t)+t(t+1).FN​(t)=

The minimum LNL_NLN​ and maximum RNR_NRN​ attainable sums are

LN=−max⁡0≤t≤NBN(t),RN=max⁡0≤t≤NFN(t).L_N=-\max_{0\leq t\leq N} B_N(t),\qquad R_N=\max_{0\leq t\leq N} F_N(t).LN​=−0≤t≤N

Both functions are concave on the integer interval. Binary search comparing adjacent values finds each maximum. Let p,qp,qp,q be the smallest indices attaining the maxima of FN,BNF_N,B_NFN​,BN​, respectively. Always choose the smallest index when values are tied.

Exact decisions for large NNN. For N≥13N\geq13N≥13, use the following reachability classification.

  • If k<LNk<L_Nk<LN​ or k>RNk>R_Nk>RN​, the target is impossible.
  • If , the target is attainable exactly when it is one of .

For small NNN, use the exact DP above instead of applying this classification directly.

After the fixed precomputation for N≤12N\leq12N≤12, each test case takes O(log⁡N)O(\log N)O(logN) time and O(1)O(1)O(1) additional space. For the interval-decision partial credit, simply compute both extrema and test LN≤k≤RNL_N\leq k\leq R_N.

Correctness proof

A tree of maximum depth hhh has vertices at every depth 0,1,…,h0,1,\ldots,h0,1,…,h. Thus, the next depth may be chosen from exactly 1,…,h+11,\ldots,h+11,…,h+1, proving the small- DP transition. Call a new vertex at depth a . Write

Solution written by GPT6

2i−1​
−
d2i​).
h
xxx
111
h+1h+1h+1
ddd
max⁡(h,d)\max(h,d)max(h,d)
ddd
N≤12N\leq12N≤12
62N3+3tN2+3tN−2N−5t3−6t2−t​
,
BN​
(
t
)
=
FN​(t)+
t(t+
1).
max
​
BN​
(
t
)
,
RN​
=
0≤t≤Nmax​FN​(t).
RN−k<p+1R_N-k<p+1
RN​−k<p+1
FN(p−1),FN(p),FN(p+1)F_N(p-1),F_N(p),F_N(p+1)FN​(p−1),FN​(p),FN​(p+1)
  • If k−LN<q+1k-L_N<q+1k−LN​<q+1, the target is attainable exactly when −k-k−k is one of BN(q−1),BN(q),BN(q+1)B_N(q-1),B_N(q),B_N(q+1)BN​(q−1),BN​(q),BN​(q+.
  • Every other integer in the interval is attainable.
  • LN​
    ≤
    k≤
    RN​
    NN
    N
    h+1h+1h+1
    record vertex
    S(x)=x(x+1)2,Q=∑i=1Ni(i−1).S(x)=\frac{x(x+1)}2,\qquad Q=\sum_{i=1}^{N}i(i-1).S(x)=2x(x+1)​,Q=i=1∑N​i(i−1).

    Bounds for a fixed record set. First maximize the original sum. Fix the indices of negative-energy record vertices as R={b1<⋯<bt}\mathcal R=\{b_1<\cdots<b_t\}R={b1​<⋯<bt​}. Nonrecord negative vertices optimally have depth 111, while nonrecord positive vertices optimally have the current maximum depth. Promoting positive vertex iii to a record increases the value by

    i 1i∉R+∑j>i, j∉Rj≥0.i\,\mathbf{1}_{i\notin\mathcal R}+\sum_{j>i,\,j\notin\mathcal R}j\geq0.i1i∈/R​+j>i,j∈/R∑​j≥0.

    When i∈Ri\in\mathcal Ri∈R, the negative record in the same pair also moves one level deeper and cancels the own term iii. Consequently, an optimum exists in which every positive vertex is a record. Its value is

    Q+∑r=1tfr(br),fr(x)=S(N)−3x2+(2r−1)x2.Q+\sum_{r=1}^{t}f_r(b_r),\qquad f_r(x)=S(N)-\frac{3x^2+(2r-1)x}{2}.Q+r=1∑t​fr​(br​),fr​(x)=S(N)−23x2+(2r−1)x​.

    Since br≥rb_r\geq rbr​≥r and fr(x)−fr(x+1)=3x+r+1>0f_r(x)-f_r(x+1)=3x+r+1>0fr​(x)−fr​(x+1)=3x+r+1>0, this is at most FN(t)F_N(t)FN​(t), obtained by the prefix set {1,…,t}\{1,\ldots,t\}{1,…,t}. A nonprefix set has bt≥t+1b_t\geq t+1bt​≥t+1 and loses at least 4t+14t+14t+1 from that bound.

    For a prefix set with t<Nt<Nt<N, omitting any positive record loses at least t+1t+1t+1. If i>ti>ti>t, its own term i≥t+1i\geq t+1i≥t+1 remains; if i≤ti\leq ti≤t, the future term j=N∉Rj=N\notin\mathcal Rj=N∈/R remains. Raising a nonrecord negative depth above 111 also loses at least t+1t+1t+1, since its index exceeds ttt. Thus every other value for this prefix set is at most FN(t)−t−1F_N(t)-t-1FN​(t)−t−1.

    For the opposite direction, negate the objective and fix the record set among the originally positive vertices. Originally positive nonrecords optimally have depth 111, and every originally negative vertex is a record at the optimum. Promoting negative vertex iii gives i+∑j>i, j∉Rj>0i+\sum_{j>i,\,j\notin\mathcal R}j>0i+∑j>i,j∈/R​j>0. The maximum is

    Q+∑r=1tgr(br),gr(x)=S(N)−3x2+(2r−5)x2.Q+\sum_{r=1}^{t}g_r(b_r),\qquad g_r(x)=S(N)-\frac{3x^2+(2r-5)x}{2}.Q+r=1∑t​gr​(br​),gr​(x)=S(N)−23x2+(2r−5)x​.

    Here gr(x)−gr(x+1)=3x+r−1>0g_r(x)-g_r(x+1)=3x+r-1>0gr​(x)−gr​(x+1)=3x+r−1>0, so the prefix set gives BN(t)B_N(t)BN​(t) and a nonprefix set loses at least 4t−14t-14t−1. For a prefix set with t<Nt<Nt<N, other changes from its optimum lose at least t+1t+1t+1. The first originally positive vertex must be a record, but BN(1)>BN(0)B_N(1)>B_N(0)BN​(1)>BN​(0), so including t=0t=0t=0 does not change the maximum. These arguments prove both extremum formulas.

    Two explicit constructions. Give the first ttt pairs depths 2i−1,2i2i-1,2i2i−1,2i. For each later i>ti>ti>t, fixing the positive depth at i+ti+ti+t and choosing the negative depth as 1+bi1+b_i1+bi​ produces

    FN(t)−∑i=t+1Nibi,0≤bi≤i+t−1.F_N(t)-\sum_{i=t+1}^{N}i b_i,\qquad 0\leq b_i\leq i+t-1.FN​(t)−i=t+1∑N​ibi​,0≤bi​≤i+t−1.

    Conversely, fixing the negative depth at i+ti+ti+t and choosing the positive depth as 1+bi1+b_i1+bi​ produces

    −BN(t)+∑i=t+1Nibi.-B_N(t)+\sum_{i=t+1}^{N}i b_i.−BN​(t)+i=t+1∑N​ibi​.

    Even if the positive vertex first reaches depth i+ti+ti+t, the negative vertex may use an existing parent at depth i+t−1i+t-1i+t−1 and stay at that same depth. Every choice of the bib_ibi​ is therefore feasible.

    A coin-coverage lemma. Suppose t≥1t\geq1t≥1 and N≥2t+1N\geq2t+1N≥2t+1. There are i+t−1i+t-1i+t−1 coins of value iii. Set a=t+1a=t+1a=t+1, let CCC be the coin count, and let WWW be their total value. Their subset sums are exactly

    {0} ∪ [a,W−a] ∪ {W}.\{0\}\ \cup\ [a,W-a]\ \cup\ \{W\}.{0} ∪ [a,W−a] ∪ {W}.

    For exactly qqq coins, every integer between the smallest-qqq sum AqA_qAq​ and largest-qqq sum ZqZ_qZq​ is attainable. Move the selected indices through the sorted coin sequence from the smallest selection to the largest. Each move changes the sum by 000 or 111, since adjacent coin values differ by at most one.

    It remains to connect consecutive intervals for q=1,…,C−2q=1,\ldots,C-2q=1,…,C−2. Since Zq=W−AC−qZ_q=W-A_{C-q}Zq​=W−AC−q​, their gap is

    Dq=Aq+1−Zq=Aq+1+AC−q−W.D_q=A_{q+1}-Z_q=A_{q+1}+A_{C-q}-W.Dq​=Aq+1​−Zq​=Aq+1​+AC−q​−W.

    For sorted coins c1≤⋯≤cCc_1\leq\cdots\leq c_Cc1​≤⋯≤cC​, we have Dq+1−Dq=cq+2−cC−qD_{q+1}-D_q=c_{q+2}-c_{C-q}Dq+1​−Dq​=cq+2​−. Hence DqD_qDq​ decreases toward the middle and then increases. Its maximum occurs at an endpoint. There are at least two smallest coins, so that maximum is 2a−N≤12a-N\leq12a−N≤1. The intervals for q=1,…,C−1q=1,\ldots,C-1q=1,…,C−1 therefore fill [a,W−a][a,W-a][a,W−a]. Choosing no coins or all coins gives the other two sums, and the minimum coin value excludes any other exterior sums.

    Completeness of the interior for large NNN. The first differences are

    ΔFt=FN(t+1)−FN(t)=N(N+1)−(t+1)(5t+4)2,\Delta F_t=F_N(t+1)-F_N(t)=\frac{N(N+1)-(t+1)(5t+4)}2,ΔFt​=FN​(t+1)−FN​(t)=2N(N+1)−(t+1)(5t+4)​,
    ΔBt=BN(t+1)−BN(t)=N(N+1)−5t(t+1)2,\Delta B_t=B_N(t+1)-B_N(t)=\frac{N(N+1)-5t(t+1)}2,ΔBt​=BN​(t+1)−BN​(t)=2N(N+1)−5t(t+1)​,

    and decrease. For N≥13N\geq13N≥13, the first maximizing indices satisfy p,q≥2p,q\geq2p,q≥2 and N≥2p+1,2q+1N\geq2p+1,2q+1N≥2p+1,2q+1. Indeed, when N=2sN=2sN=2s, ΔBs−1=s(7−s)/2≤0\Delta B_{s-1}=s(7-s)/2\leq0ΔBs−1​=s(7−s)/2≤0; when N=2s+1N=2s+1N=2s+1, ΔBs=−(s−2)(s+1)/2≤0\Delta B_s=-(s-2)(s+1)/2\leq0ΔBs​=−(s−2)(s+1)/2≤0. Also ΔFt<ΔBt\Delta F_t<\Delta B_tΔFt​<ΔBt​.

    Apply the coin lemma to the first construction at t=pt=pt=p. It fills

    [−S(p)+p+1, RN−p−1].[-S(p)+p+1,\ R_N-p-1].[−S(p)+p+1, RN​−p−1].

    The left endpoint is at most 111, so every positive target in the interior is attainable. The second construction at t=qt=qt=q fills

    [LN+q+1, −S(q)−q−1].[L_N+q+1,\ -S(q)-q-1].[LN​+q+1, −S(q)−q−1].

    Meanwhile, putting every positive vertex at depth 111 and each negative vertex independently at depth 111 or 222 fills [−S(N),0][-S(N),0][−S(N),0]. The two intervals overlap, filling the negative interior as well. Therefore every integer in

    [LN+q+1, RN−p−1][L_N+q+1,\ R_N-p-1][LN​+q+1, RN​−p−1]

    is attainable.

    The three candidates at either boundary. Consider the upper band [RN−p,RN][R_N-p,R_N][RN​−p,RN​]. We have ΔFt−ΔFt−1=−(5t+2)\Delta F_t-\Delta F_{t-1}=-(5t+2)ΔFt​−ΔFt−1​=−(5t+2). Since ppp is the first maximizer, ΔFp−1>0\Delta F_{p-1}>0ΔFp−1​>0 and ΔFp≤0\Delta F_p\leq0ΔFp​≤0, giving

    FN(p)−FN(p−2)>5(p−1)+2>p,F_N(p)-F_N(p-2)>5(p-1)+2>p,FN​(p)−FN​(p−2)>5(p−1)+2>p,
    FN(p)−FN(p+2)≥5(p+1)+2>p.F_N(p)-F_N(p+2)\geq5(p+1)+2>p.FN​(p)−FN​(p+2)≥5(p+1)+2>p.

    If the number ttt of negative records is outside p−1,p,p+1p-1,p,p+1p−1,p,p+1, even its optimum is outside the band. For these three counts, nonprefix record sets lose at least 4t+1>p4t+1>p4t+1>p and are excluded too. Any other modification of a prefix optimum loses at least t+1t+1t+1. When t=p−1t=p-1t=p−1, the first-maximizer convention gives FN(t)<RNF_N(t)<R_NFN​(t)<RN​, leaving a loss budget of at most p−1=tp-1=tp−1=t; the budget is also at most ttt in the other two cases. Thus only FN(p−1),FN(p),FN(p+1)F_N(p-1),F_N(p),F_N(p+1)FN​(p−1),FN​(p),F can occur in the band. The explicit constructions attain each candidate that lies there.

    The lower band follows in the same way from ΔBt−ΔBt−1=−5t\Delta B_t-\Delta B_{t-1}=-5tΔBt​−ΔBt−1​=−5t, using qqq instead of ppp. Moving two indices away loses more than 5(q−1)5(q-1)5(q−1) or at least 5(q+1)5(q+1)5(q+1), both exceeding qqq. Nonprefix sets lose at least 4t−1>q4t-1>q4t−1>q. Hence only −BN(q−1),−BN(q),−BN(q+1)-B_N(q-1),-B_N(q),-B_N(q+1)−BN​(q−1),−BN​(q can occur. This completes the reachability classification for N≥13N\geq13N≥13.

    1)
    cC−q​
    N​
    (
    p
    +
    1)
    )
    ,
    −
    BN​
    (
    q
    +
    1)