Merge Piles


You are given a sequence of NN piles. The ii-th pile contains aia_i stones.

In one move, you may merge two adjacent groups of piles into one larger group. The cost of the move is the total number of stones in the newly merged group.

After exactly N1N - 1 moves, all piles will be merged into one group. Find the minimum possible total cost.

Input

The first line contains an integer NN, the number of piles.

The second line contains NN integers: a1,a2,,aNa_1, a_2, \ldots, a_N.

Output

Print one integer: the minimum possible total cost to merge all piles into one group.

Constraints

  • 1N2501 \le N \le 250
  • 1ai1061 \le a_i \le 10^6

Example 1

Input 1
4
10 20 30 40
Output 1
190
Explanation

One optimal sequence is:

  • Merge piles with 1010 and 2020 stones, paying 3030.
  • Merge the new group with the pile containing 3030 stones, paying 6060.
  • Merge the new group with the pile containing 4040 stones, paying 100100.

The total cost is 30+60+100=19030 + 60 + 100 = 190.

Example 2

Input 2
5
1 100 1 100 1
Output 2
507

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.