Joining Wires
You have wires of given lengths. Joining two wires of lengths and costs and gives one wire of length . Join them all into one wire at minimum total cost.
Always join the two shortest — a min-heap makes that fast.
Input
, then lengths.
Output
The minimum total cost.
Constraints
, length — use 64-bit totals.
Sample 1
Input
4 4 3 2 6
Output
29
Sample 2
Input
1 5
Output
0