Loading...
Loading...
Loading...
A workshop has n cables. Joining two cables of lengths a and b costs a + b and gives one cable of length a + b. Print the smallest total cost to join all cables into one. For a single cable the cost is 0.
Example 1
Input
4 4 3 2 6
Output
29
Join 2 and 3 (5), 4 and 5 (9), 6 and 9 (15): 29.
Example 2
Input
1 5
Output
0
Try the problem first. Open one hint at a time.
Always join the two shortest cables, because their length is paid again in later joins.
A min-heap gives the two shortest cables.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor