Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/dp-matritsalar-zanjiri

Matrix chain

Medium 1000 ms 128 MB 64%
Share

$n$ matrices must be multiplied in order: the size of the $i$-th matrix is $p_{i-1} \times p_{i}$. Multiplying two matrices of sizes $a \times b$ and $b \times c$ takes $a * b * c$ operations. The order of multiplication (the placement of brackets) may be chosen, but the matrices cannot be swapped. What is the smallest number of operations needed?

A chain of matrices: rectangles with their sizes, the cheapest bracketing highlighted

Input

The first line contains a positive integer $n$. The second line contains $n + 1$ positive integers: $p_{0}, p_{1}, \ldots, p_{n}$.

Output

One integer — the smallest number of operations.

Note

$1 \le n \le 100$
$1 \le p_{i} \le 100$

Examples

Input #1
3
10 30 5 60
Output #1
4500
Input #2
1
4 7
Output #2
0

Sign in to submit a solution

Signing up is free and takes a minute.