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

KMO

Back to problem

Editorial

Tree interpretation

Lengths bib_ibi​ of a prefix code over an alphabet of size KKK satisfy Kraft's inequality ∑K−bi≤1\sum K^{-b_i}\leq1∑K−bi​≤1, and conversely every such integer length sequence can be realized. To see the converse, process lengths in increasing order, expanding each unused position into KKK positions at the next depth and placing the required leaves.

Thus we seek a rooted tree with at most KKK children per internal node, minimizing the sum of leaf weight times leaf depth. Zero-weight input elements remain real leaves and must not be discarded.

Huffman merging

If N=1N=1N=1, output 000. Otherwise a full KKK-ary tree has a leaf count congruent to 111 modulo K−1K-1K−1. Add

d=(K−1−(N−1) mod (K−1)) mod (K−1)d=(K-1-(N-1)\bmod(K-1))\bmod(K-1)d=(K−1−(N−1)mod

dummy leaves of weight zero. This preserves the optimum: remove unary nodes from an optimal tree, and consolidate incomplete internal nodes at the deepest level until only one incomplete node remains. Moving leaves toward the root cannot increase the cost for nonnegative weights. Filling its missing positions requires exactly these ddd dummy leaves.

There is an optimal full tree whose KKK lightest leaves are deepest siblings. Exchanging a lighter shallow leaf with a heavier deep leaf cannot increase cost. Contract these siblings into one leaf whose weight is their sum. The original cost equals the contracted cost plus that sum. Repeating this operation gives KKK-ary Huffman coding.

Avoid constructing dummy leaves explicitly. Merge

r=K−d=2+(N−2) mod (K−1)r=K-d=2+(N-2)\bmod(K-1)r=K−d=2+(N−2)mod(

real nodes first, and merge exactly KKK nodes in every later step. In particular, K>NK>NK>N gives r=Nr=Nr=N. If N−1N-1N−1 is divisible by , the first merge must also use nodes.

Use a min-heap. Record the newly created node as the parent of each removed node. Parent indices are larger than child indices, so compute depths in reverse node order without recursion. Output the original leaves' depths in input order.

Bounds and complexity

There are at most N−1N-1N−1 internal nodes, each with at least two children. Thus every leaf depth is at most N−1N-1N−1. Time is O(Nlog⁡N)O(N\log N)O(NlogN) and space is O(N)O(N).

A merged weight is at most ∑ai≤2⋅1014\sum a_i\leq2\cdot10^{14}∑ai​≤2⋅1014. Assigning every leaf depth ⌈log⁡KN⌉\lceil\log_K N\rceil⌈ is feasible, so the optimum is at most . Signed 64-bit integers suffice; never compute directly.

Subtasks

For subtask 1, sort weights in decreasing order and enumerate nondecreasing depths from 000 through N−1N-1N−1. An exchange argument guarantees an optimum with this ordering. There are (2N−1N)\binom{2N-1}{N}(N2N−1​ sequences, each checked in , giving time.

Subtask 2 is binary Huffman coding: repeatedly merge the two smallest weights in O(Nlog⁡N)O(N\log N)O(NlogN) time.

Exact certificate checking

Count leaves at each depth. Start with one position at depth zero, subtract leaves placed there, and multiply remaining positions by KKK before processing the next depth. Reject if a depth needs too many positions. Cap the position count at N+1N+1N+1: there are only NNN leaves, so larger exact counts cannot affect feasibility.

The checker recomputes the optimum and uses it as a remaining cost budget. Before subtracting aibia_i b_iai​bi​, use division to detect an exceeded budget. This avoids overflow even for incorrect contestant output.

Solution written by GPT6

(K−
1))mod
(K−
1)
K
−
1)
K−1K-1
K−1
KKK
O(N)
log
K​
N
⌉
3.6⋅10153.6\cdot10^{15}3.6⋅1015
KbiK^{b_i}Kbi​
)
O(N)O(N)O(N)
O(N(2N−1N))O(N\binom{2N-1}{N})O(N(N2N−1​))