#K88737. Minimum Cost to Merge Ropes
Minimum Cost to Merge Ropes
Minimum Cost to Merge Ropes
You are given n ropes with lengths \(a_1, a_2, \ldots, a_n\). Your task is to merge these ropes into one rope. The cost of merging two ropes of lengths \(L_1\) and \(L_2\) is \(L_1 + L_2\). You need to determine the minimum total cost required to merge all the ropes into one.
Explanation: At each step, choose the two smallest ropes and merge them. This greedy approach minimizes the total cost. For example, if you have ropes with lengths 4, 3, 2, and 6, you would merge them in the optimal order leading to a total cost of 29.
inputFormat
The input is read from standard input (stdin) and consists of two lines. The first line contains a single integer (n) (the number of ropes). The second line contains (n) space-separated positive integers representing the lengths of the ropes.
outputFormat
Output to standard output (stdout) a single integer, which is the minimum total cost to merge all the ropes into one.## sample
1
5
0
</p>