#C9986. Minimal Rope Combination Cost
Minimal Rope Combination Cost
Minimal Rope Combination Cost
Given n ropes with lengths \(L_1, L_2, \dots, L_n\), your task is to combine them into a single rope with the minimum total cost. The cost to combine two ropes is equal to the sum of their lengths. You can combine ropes in any order. To achieve the optimal solution, use a greedy algorithm by always combining the two shortest ropes first.
Input Format: The first line contains an integer \(n\) (the number of ropes). The second line contains \(n\) space-separated integers representing the lengths of the ropes.
Output Format: Output a single integer representing the minimal total cost to combine all ropes into one.
inputFormat
The input is given via standard input with the following format:
n L1 L2 L3 ... Ln
where \(1 \leq n \leq 10^5\) and each \(L_i\) is a positive integer representing the length of a rope.
outputFormat
Output a single integer, which is the minimal total cost of merging all the ropes into one.
## sample4
4 3 2 6
29