Skip to content
UPEEC

Matrix Chain Multiplication

Hard#dynamic-programming#interval-dp· solved by 0 · time limit 2.0 s

Matrices A1…AnA_1 \dots A_n have sizes d0×d1,d1×d2,…,dn−1×dnd_0 \times d_1, d_1 \times d_2, \dots, d_{n-1} \times d_n. Multiplying a p×qp \times q by a q×rq \times r matrix costs p⋅q⋅rp \cdot q \cdot r. Find the minimum total cost to compute the whole product (choose the best bracketing).

Input

nn, then the n+1n+1 dimensions d0…dnd_0 \dots d_n.

Output

The minimum cost.

Constraints

1≤n≤2001 \le n \le 200, 1≤di≤1001 \le d_i \le 100

Sample 1

Input

3
10 30 5 60

Output

4500

Sample 2

Input

4
40 20 30 10 30

Output

26000

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