luogu#P8311. [COCI 2021/2022 #4] Autići

[COCI 2021/2022 #4] Autići

Problem Description

There are nn good friends. Each person has a remote-controlled car and a garage. The ii-th person has several toy road pieces of length did_i, which can be used to build roads for the cars.

Two friends aa and bb can build a road of length da+dbd_a + d_b to connect their garages.

We say that the traffic is "connected" if, starting from any garage, it is possible to reach any other garage.

Find the minimum total road length needed to achieve such "connected traffic".

Input Format

The first line contains an integer nn, the number of friends.

The second line contains nn integers did_i, where did_i is the length of the road pieces owned by the ii-th friend.

Output Format

Output a single line: the minimum total road length needed to achieve "connected traffic".

1
10
0
3
5 5 5
20
4
7 3 3 5
24

Hint

[Explanation for Sample 1]

When there is only one friend, the traffic is already "connected", so there is no need to build any road. Therefore, the answer is 00.

[Explanation for Sample 3]

If roads are built between friend 11 and friend 22, between friend 22 and friend 33, and between friend 33 and friend 44, a "connected traffic" can be formed. The total cost is (7+3)+(3+3)+(3+5)=24(7+3) + (3+3) + (3+5) = 24.

[Constraints]

This problem uses bundled subtasks.

  • Subtask 1 (10 pts): d1=d2==dnd_1 = d_2 = \dots = d_n.
  • Subtask 2 (20 pts): 1n1031 \le n \le 10^3.
  • Subtask 3 (20 pts): No additional constraints.

For 100%100\% of the testdata, 1n1051 \le n \le 10^5, 1di1091 \le d_i \le 10^9.

[Hints and Notes]

The scoring of this problem follows the original COCI problem settings, with a full score of 5050.

Translated from COCI2021-2022 CONTEST #4 T1 Autići.

Translated by ChatGPT 5