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

Zygarde Gyeonggwak Forme

Back to problem

Editorial

  1. Reducing the tree to groups on the Core path

Let V1,V2,…,VMV_1,V_2,\ldots,V_MV1​,V2​,…,VM​ be the vertices on the path from Core AAA to Core ZZZ, in order. Assign each vertex to its unique closest vertex on the Core path, and let wiw_iwi​ be the number assigned to ViV_iVi​. This group includes ViV_iVi​ itself, so every wiw_iwi​ is positive.

Only edges on the Core path may be deleted. Consequently, vertices in the same group always stay together, and each resulting component consists of a contiguous interval of groups. Conversely, any partition of the MMM groups into exactly KKK nonempty contiguous intervals can be obtained by deleting the K−1K-1K−1 edges between those intervals.

A pair of vertices in the same group contributes zero energy. For two different groups iii and jjj, there are wiwjw_iw_jwi​wj​ vertex pairs, each contributing . Therefore, the cost of making groups through one component is

C(l,r)=∑l≤i<j≤rwiwj(j−i).C(l,r)=\sum_{l\le i<j\le r}w_iw_j(j-i).C(l,r)=l≤i<j≤r∑​w

To find the groups, root the original tree at AAA and compute each vertex's parent together with a traversal order in which every parent precedes its children. Follow parents from ZZZ to recover the Core path. First assign group indices to all path vertices. Then traverse the other vertices in the recorded order and give each vertex its parent's group index. This takes O(N)O(N)O(N) time.

  1. Evaluating an interval cost in O(1)O(1)O(1)

Define the following prefix sums, with all values at index zero equal to zero:

Wi=∑j=1iwj,Pi=∑j=1ijwj,Hi=C(1,i).W_i=\sum_{j=1}^{i}w_j,\qquad P_i=\sum_{j=1}^{i}j w_j,\qquad H_i=C(1,i).Wi​=j=1∑

When adding group iii, only pairs between that group and earlier groups are new. Thus,

Hi=Hi−1+wi(iWi−1−Pi−1).H_i=H_{i-1}+w_i(iW_{i-1}-P_{i-1}).Hi​=Hi−1​+

Set t=l−1t=l-1t=l−1. The difference Hr−HtH_r-H_tHr​−Ht includes both the cost within and the cost of pairs with one group in and the other in . Subtracting the latter gives

C(l,r)=Hr−Ht−Wt(Pr−Pt)+Pt(Wr−Wt).C(l,r)=H_r-H_t-W_t(P_r-P_t)+P_t(W_r-W_t).C(l,r)=Hr​−H

After O(M)O(M)O(M) preprocessing, this formula evaluates any interval cost in O(1)O(1)O(1).

  1. Dynamic programming

Let DPk[r]DP_k[r]DPk​[r] be the minimum cost of partitioning the first rrr groups into exactly kkk nonempty contiguous intervals. Enumerating the last interval [l,r][l,r][ gives

DPk[r]=min⁡k≤l≤r{DPk−1[l−1]+C(l,r)}.DP_k[r]=\min_{k\le l\le r}\{DP_{k-1}[l-1]+C(l,r)\}.DPk​[r]=k≤l

Initialize DP0[0]=0DP_0[0]=0DP0​[0]=0 and all impossible states to infinity. The answer is DPK[M]DP_K[M]DPK​[. Directly evaluating this recurrence takes time.

  1. The Monge property and divide-and-conquer optimization

For a≤b≤c≤da\le b\le c\le da≤b≤c≤d, expanding the following difference yields

C(a,d)+C(b,c)−C(a,c)−C(b,d)=∑i=ab−1∑j=c+1dwiwj(j−i)≥0.C(a,d)+C(b,c)-C(a,c)-C(b,d)=\sum_{i=a}^{b-1}\sum_{j=c+1}^{d}w_iw_j(j-i)\ge0.C(a,d)+C(b,c)−C

Thus, the interval costs satisfy the Monge inequality. Adding DPk−1[l−1]DP_{k-1}[l-1]DPk−1​[l−1] to each candidate lll preserves this inequality. It follows that the optimal starting position of the last interval does not decrease as rrr increases. Choosing the smallest starting position in a tie gives a consistent nondecreasing sequence of optimal positions.

To compute one DPDPDP layer, first process the middle endpoint midmidmid. If the possible optimal starting positions lie in [L,R][L,R][L,R], inspect lll from LLL through and find an optimum . Recurse on the left half with candidate range , and on the right half with candidate range . For layer , start with endpoint range and candidate range . Ignore candidates whose previous-layer state is impossible.

The recursion has O(log⁡M)O(\log M)O(logM) levels, and the total number of inspected candidates at each level is O(M)O(M)O(M). One layer therefore takes O(Mlog⁡M)O(M\log M)O(MlogM) time.

  1. Complexity and implementation details

The total time complexity is O(N+KMlog⁡M)O(N+KM\log M)O(N+KMlogM), and the space complexity is O(N)O(N)O(N). Only the previous and current DPDPDP layers need to be stored.

There are at most N(N−1)/2N(N-1)/2N(N−1)/2 vertex pairs, and each pair contributes at most M−1M-1M−1, so the total energy is less than 4×10154\times10^{15}4×10. Also, and , so . The intermediate sums and differences in the cost formula fit in 64-bit integers as well. Use 64-bit integers for , , , , the values, and intermediate products. The original tree being unweighted does not make a 32-bit answer sufficient.

The tree can be a path with NNN vertices. An iterative traversal avoids a stack overflow while building the groups. The divide-and-conquer DPDPDP itself has only O(log⁡M)O(\log M)O(logM) recursion depth.

∣i−j∣|i-j|
∣i−j∣
lll
rrr
i​
wj​
(
j
−
i).
i
​
wj​
,
Pi​
=
j=1∑i​jwj​,Hi​=
C(1,i).
w
i​
(
i
Wi−1​
−
Pi−1​).
​
[l,r][l,r][l,r]
[1,t][1,t][1,t]
[l,r][l,r][l,r]
t​
−
Wt​(Pr​−
Pt​)+
Pt​(Wr​−
Wt​).
l
,
r
]
≤
r
min
​
{
D
Pk−1​
[
l
−
1]+
C(l,r)}.
M
]
O(KM2)O(KM^2)O(KM2)
(
a
,
c
)
−
C(b,d)=
i=a∑b−1​j=c+1∑d​wi​wj​(j−
i)≥
0.
min⁡(mid,R)\min(\mathrm{mid},R)
min(mid,R)
optoptopt
[L,opt][L,\mathrm{opt}][L,opt]
[opt,R][\mathrm{opt},R][opt,R]
kkk
[k,M][k,M][k,M]
[k,M][k,M][k,M]
15
Wi≤NW_i\le NWi​≤N
Pi≤NMP_i\le NMPi​≤NM
WiPj≤N2M≤8×1015W_iP_j\le N^2M\le 8\times10^{15}Wi​Pj​≤N2M≤8×1015
www
WWW
PPP
HHH
DPDPDP