Skip to content
UPEEC

Joining Wires

Medium#greedy#heap· solved by 0 · time limit 2.0 s

You have nn wires of given lengths. Joining two wires of lengths xx and yy costs x+yx + y and gives one wire of length x+yx + y. Join them all into one wire at minimum total cost.

Always join the two shortest — a min-heap makes that fast.

Input

nn, then nn lengths.

Output

The minimum total cost.

Constraints

1≤n≤1051 \le n \le 10^5, 1≤1 \le length ≤104\le 10^4 — use 64-bit totals.

Sample 1

Input

4
4 3 2 6

Output

29

Sample 2

Input

1
5

Output

0

Sign in or join free to run and submit your solution.