# Lexicographically Largest Heap Labels Estimated time limit: 2.5 seconds. Memory limit: 512 MB. The actual limit is the reference program's slowest median runtime on the target platform multiplied by $4$ , rounded up to a multiple of $0.5$ seconds. See the [evaluation environment](../../../docs/评测环境与性能.md). This multiplier provides at least twice the measured runtime of the static-sorting alternative. ## Story A gallery is decorating a tree with lamps of different brightnesses. Every lamp must be brighter than its ancestors. When inspecting vertices by number, the curator wants the earliest possible vertices to be as bright as possible. (This story was rewritten by GPT-6.) ## Formal Problem Statement A tree has $N$ vertices numbered $1,\ldots,N$ and root $K$ . Assign the integers $1,\ldots,N$ bijectively as labels $a_1,\ldots,a_N$ . Every nonroot vertex must have a strictly larger label than its parent. Among valid assignments, maximize the array $(a_1,\ldots,a_N)$ lexicographically: at the first vertex number where two assignments differ, the larger label makes the larger assignment. For the optimal permutation, define its zero-based Cantor rank by $$R=\sum_{i=1}^{N} c_i (N-i)!,\qquad c_i=\#\{j:i