cswork / OAOpen practice workspace

JPMorgan ChaseOA #31

Reduce the Array (Also for DA)

Estimated MediumJudge-ready

Topics (AI-labelled)Stacks & HeapsGreedy

Each time, choose elements at two different positions, remove them, and insert their sum; the cost of this step is that sum. Find the minimum total cost to merge everything into a single element.

Input

The first line contains n, followed by n positive integers; full original bounds 2≤n≤100000, 1≤a[i]≤100.

Output

Output a single integer answer.

Example 1

Input:

3
25 10 20

Output:

85

Example 2

Input:

2
1 1

Output:

2

Example 3

Input:

4
2 3 4 5

Output:

28

Example notes

The outputs of the three public examples are, in order: 85; 2; 28. These answers have been checked by independent enumeration or direct simulation.

Editorial

Loading solution…