Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/ochkoz-arqonlarni-ulash

Joining ropes

Medium 1000 ms 128 MB 51%
Share

There are $n$ ropes, the length of the $i$-th one is $a_{i}$. In one operation any two ropes are joined; joining ropes of lengths $x$ and $y$ costs $x + y$ soums and produces one rope of length $x + y$. What is the smallest amount of money needed to join all the ropes into one?

Rope lengths; at each step the two shortest are joined, with the joining costs shown

Input

The first line contains a positive integer $n$. The second line contains $n$ space-separated positive integers $a_{i}$.

Output

One integer — the smallest total cost.

Note

$1 \le n \le 10^{5}$
$1 \le a_{i} \le 10^{9}$

Examples

Input #1
4
4 3 2 6
Output #1
29
Input #2
1
10
Output #2
0

Sign in to submit a solution

Signing up is free and takes a minute.