LogiQuest

399. Connect Ropes at Minimum Cost

MediumHeapGreedy

Connecting two ropes of lengths a and b costs a + b and makes a rope of length a + b. Print the minimum total cost to connect all ropes into one.

Input format

Line 1: n Line 2: n lengths

Output format

The minimum cost.

Example 1

Input

4
4 3 2 6

Output

29

Example 2

Input

1
5

Output

0

Constraints

1 ≤ n ≤ 10⁵

Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.

Loading the MyLoginn IDE…