In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of January 4th
(this exercise will still be available for submission after that deadline, but without couting towards your grade)
[to understand the context of this problem, you should read the class #12 exercise sheet]


[AED073] Rope Connections

Leo has N ropes of different lengths and he wants to tie them all into a single big rope. The cost to connect two ropes is equal to sum of their lengths. Of course Leo wants to connect all the ropes with the minimum cost possible!

For example, suppose we have 4 ropes of lengths 7, 3, 5, and 1:

Can you help Leo discover how to connect the ropes for a general cases?

The Problem

Given a list of N lengths of ropes, your task is to compute the minimum cost possible to form a single large rope assuming the cost to connect two ropes is equal to sum of their lengths.

Input

The first line of input contains an integer N, indicating the number of ropes to consider.

The second line contains N integers L1, L2, ..., XN, the lengths of the ropes (note that some lengths might be equal).

Output

The output should be a line containing the minimum possible cost to join all the ropes into a single rope.

Constraints

The following limits are guaranteed in all the test cases that will be given to your program:

1 ≤ N ≤ 100 000       Number of ropes
1 ≤ Li ≤ 1 000       Length of each rope

Example Input 1 Example Output 1
4
7 3 5 1
29

Explanation of Example Input 1

This example is explained in the problem statement.


Example Input 1 Example Output 1
5
18 2 2 2 2
42

Explanation of Example Input 2

One way to obtain cost 42 would be:

The total cost is 4+4+8+26=42.



Algorithms and Data Structures (L.EIC011) 2024/2025
DCC/FCUP & DEI/FEUP - University of Porto