Hints: Building Tree
Answer to Hint 1: The path length is the MEX of the weights on the path, so weight $k$ is absent from the path.
Therefore, if two original vertices can be connected by a path that avoids all edges of weight $k$, then their distance is at most $k$.
Answer to Hint 3: If there is a path from $u$ to $v$ with MEX $k$, then that path has no edge of weight $k$. So $u$ and $v$ are connected after deleting all edges of weight $k$.
This gives the key formula:
$$ \mathrm{dis}(u,v)=\min \{k : u \text{ and } v \text{ are connected after deleting all weight-}k\text{ edges}\}. $$Answer to Hint 5: Every edge weight is between $0$ and $m$. Also, any path has at most $m$ edges, but there are $m+1$ possible weights, so the path misses at least one weight from $0$ to $m$.
Thus if two vertices are connected at all, their distance is some value in $[0,m]$.
Answer to Hint 7: We need a minimum spanning tree on the selected colors, where the edge cost between colors $a$ and $b$ is $\mathrm{dis}(a,b)$.
If the same color appears several times among the $c_i$, those new vertices can be connected to each other with cost $0$, so we only need to connect the distinct colors.
Answer to Hint 9: Process possible costs $k$ in increasing order. For a fixed $k$, build the connected components of the original graph after deleting all edges of weight $k$.
Inside one such component, every selected color can be connected to every other selected color with cost at most $k$. So in Kruskal, all current MST components represented inside this original component may be merged using edges of cost $k$.
Answer to Hint 11: Rebuild a DSU on the original $n$ vertices using all edges whose weight is not $k$.
Then group the distinct selected colors by their DSU root. For every group, merge the corresponding color-components in the answer DSU, adding $k$ for each successful merge.
Answer to Hint 13: At the current cost $k$, all colors in the same original DSU component form a clique of available edges of cost at most $k$.
Kruskal only needs enough edges to merge the currently different answer components in that clique. A star from one representative color is enough to merge them all, and each successful DSU union contributes exactly one MST edge of cost $k$.
After processing all $k$, if the answer DSU has not connected all distinct colors, output $-1$. Otherwise, the accumulated sum is the minimum total time.
The official optimized solution keeps the same Kruskal idea, but avoids rebuilding DSU for every $k$ by using divide-and-conquer over weights together with rollback DSU.