#d2844. 切割

切割

Cutting

Problem Source

Original problem: [NOIP 2004 Advanced Group] Merging Fruits / [USACO06NOV] Fence Repair G (https://www.luogu.com.cn/problem/P1090)

A data-strengthened version requiring an O(n)O(n) solution: (https://www.luogu.com.cn/problem/P6033) (1≤n≤1071 \le n \le 10^7)

Problem Description

There is a wooden board of total length ∑ai\sum a_i that needs to be cut into nn pieces of lengths a1,a2,…,ana_1, a_2, \dots, a_n.

Each cut incurs a cost equal to the length of the board being cut.

Find the minimum total cost to cut the board into the nn pieces above.

Input Format

The first line contains a positive integer nn.

The second line contains nn positive integers: a1,a2,…,ana_1, a_2, \dots, a_n.

Output Format

Output one positive integer: the minimum cost.

Sample Input

5
5 3 4 4 4

Sample Output

47

Constraints

For all test data: 1≤n,ai≤1051 \le n, a_i \le 10^5.