Let be the depth of . 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
Exact decisions for small . After processing , store whether a state with maximum depth 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
The minimum and maximum attainable sums are
Both functions are concave on the integer interval. Binary search comparing adjacent values finds each maximum. Let be the smallest indices attaining the maxima of , respectively. Always choose the smallest index when values are tied.
Exact decisions for large . For , use the following reachability classification.
For small , use the exact DP above instead of applying this classification directly.
After the fixed precomputation for , each test case takes time and additional space. For the interval-decision partial credit, simply compute both extrema and test .
Correctness proof
A tree of maximum depth has vertices at every depth . Thus, the next depth may be chosen from exactly , proving the small- DP transition. Call a new vertex at depth a . Write
Solution written by GPT6
Bounds for a fixed record set. First maximize the original sum. Fix the indices of negative-energy record vertices as . Nonrecord negative vertices optimally have depth , while nonrecord positive vertices optimally have the current maximum depth. Promoting positive vertex to a record increases the value by
When , the negative record in the same pair also moves one level deeper and cancels the own term . Consequently, an optimum exists in which every positive vertex is a record. Its value is
Since and , this is at most , obtained by the prefix set . A nonprefix set has and loses at least from that bound.
For a prefix set with , omitting any positive record loses at least . If , its own term remains; if , the future term remains. Raising a nonrecord negative depth above also loses at least , since its index exceeds . Thus every other value for this prefix set is at most .
For the opposite direction, negate the objective and fix the record set among the originally positive vertices. Originally positive nonrecords optimally have depth , and every originally negative vertex is a record at the optimum. Promoting negative vertex gives . The maximum is
Here , so the prefix set gives and a nonprefix set loses at least . For a prefix set with , other changes from its optimum lose at least . The first originally positive vertex must be a record, but , so including does not change the maximum. These arguments prove both extremum formulas.
Two explicit constructions. Give the first pairs depths . For each later , fixing the positive depth at and choosing the negative depth as produces
Conversely, fixing the negative depth at and choosing the positive depth as produces
Even if the positive vertex first reaches depth , the negative vertex may use an existing parent at depth and stay at that same depth. Every choice of the is therefore feasible.
A coin-coverage lemma. Suppose and . There are coins of value . Set , let be the coin count, and let be their total value. Their subset sums are exactly
For exactly coins, every integer between the smallest- sum and largest- sum is attainable. Move the selected indices through the sorted coin sequence from the smallest selection to the largest. Each move changes the sum by or , since adjacent coin values differ by at most one.
It remains to connect consecutive intervals for . Since , their gap is
For sorted coins , we have . Hence decreases toward the middle and then increases. Its maximum occurs at an endpoint. There are at least two smallest coins, so that maximum is . The intervals for therefore fill . 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 . The first differences are
and decrease. For , the first maximizing indices satisfy and . Indeed, when , ; when , . Also .
Apply the coin lemma to the first construction at . It fills
The left endpoint is at most , so every positive target in the interior is attainable. The second construction at fills
Meanwhile, putting every positive vertex at depth and each negative vertex independently at depth or fills . The two intervals overlap, filling the negative interior as well. Therefore every integer in
is attainable.
The three candidates at either boundary. Consider the upper band . We have . Since is the first maximizer, and , giving
If the number of negative records is outside , even its optimum is outside the band. For these three counts, nonprefix record sets lose at least and are excluded too. Any other modification of a prefix optimum loses at least . When , the first-maximizer convention gives , leaving a loss budget of at most ; the budget is also at most in the other two cases. Thus only can occur in the band. The explicit constructions attain each candidate that lies there.
The lower band follows in the same way from , using instead of . Moving two indices away loses more than or at least , both exceeding . Nonprefix sets lose at least . Hence only can occur. This completes the reachability classification for .