← All problemsSign in

Beauty of Pairs

CodeChefRating 2834Open on judge ↗

You are given a tree with $N$ vertices numbered 1 through $N$ as a sequence of $N-1$ edges — pairs $(a_1, b_1), (a_2, b_2), \ldots, (a_{N-1}, b_{N-1})$. Let's define the *value* of a sequence $A$ with length $N-1$ as $$F(A)=\sum_{i=1}^{N-1} A_i \cdot W_i \,,$$ where $W_1, W_2, \ldots, W_{N-1}$ are given weights. You may change the order of the pairs and the order of elements within any pairs. For

HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17

CodeSearch Tutor

Hints, not spoilers — it won’t hand over the full solution unless you insist.

voice by Sarvam AI

Sign in to chat with the tutor and save your progress.

Sign in to start