Problem 4018 · Hard

Total Sum of Interaction Cost in Tree Groups II

Each tree edge is charged once for every same-group pair it separates; counting group sizes below each node adds those charges up.

Open the interactive visualisation