Processing math: 100%
Merging elements - MarisaOJ: Marisa Online Judge

Merging elements

Time limit: 1000 ms
Memory limit: 256 MB

You are given an array A of n integers. Merging 2 adjacent elements Ai and Ai+1 costs Ai+Ai+1.

Perform the above operation until there is only 1 element left in A, what is the minimum cost?

Input

  • The first line contains an integers n.
  • The second line contains n integers Ai.

Output

  • Print the minimum cost.

Constraints

  • 1≤n≤500.
  • 1≤Ai≤109.

Example

Input:

4
10 20 30 40

Output:

190

The merging happens as follows (bolded elements are merged elements):

  • (10, 20, 30, 40) → (30, 30, 40)
  • (30, 30, 40) → (60, 40)
  • (60, 40) → (100)