#K91647. Minimize Cost to Merge Elements

    ID: 38022 Type: Default 1000ms 256MiB

Minimize Cost to Merge Elements

Minimize Cost to Merge Elements

Given an array of integers, merge the elements repeatedly until only one remains. In each merge operation, the two smallest elements are selected, summed, and the resulting sum is added to the total cost. The final merged element is the result of all these sequential merge operations.

This process is analogous to the optimal merge pattern and can be implemented efficiently using a min-heap (priority queue). The merge operation follows the formula: \( cost = a + b \), where \( a \) and \( b \) are the two smallest elements chosen at each step.

inputFormat

The first line contains an integer \(n\) representing the number of elements. The second line contains \(n\) space-separated integers.

outputFormat

Print two integers separated by a space: the final merged element and the total cost incurred during the merge operations.

## sample
5
4 2 1 3 6
16 35