Editorial for COCI '21 Contest 4 #1 Autići


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

We'll show that it's always best to connect the garage with the minimum d_i with the remaining n-1 garages.

To connect all of them, we need at least n-1 roads (initially there is only one garage, and for each additional one, we need one more road to connect it with the rest). In the optimal solution, we will build exactly n-1 roads because it is never beneficial to connect two garages if there is already a path between them.

Therefore, the total length will be an expression consisting of 2(n-1) terms. Each garage needs to be connected to at least one road, so each of the numbers d_1, d_2, \dots, d_n must appear at least once in this expression. The smallest possible value of the remaining n-2 terms is precisely the value of the smallest d_i. Notice that this is actually achieved if we connect the garage with the smallest d_i with the rest.


Comments

There are no comments at the moment.