Under construction

Still being built. Things may change

Matrix Multiplication Order 2

Time limit1sMemory limit512 MB

Summary
Given N matrix sizes in a fixed order, find the minimum number of scalar multiplications needed to compute their chain product.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy
Solved
No attempts yet

Problem

A matrix AA of size N×MN \times M and a matrix BB of size M×KM \times K need N×M×KN \times M \times K multiplications to multiply. Multiplying NN matrices takes a different number of multiplications depending on the order in which they are multiplied.

For example, suppose AA is 5×35 \times 3, BB is 3×23 \times 2, and CC is 2×62 \times 6, and consider computing the product ABCABC.

  • Multiplying ABAB first and then CC, (AB)C(AB)C takes 5×3×2+5×2×6=30+60=905 \times 3 \times 2 + 5 \times 2 \times 6 = 30 + 60 = 90 multiplications.
  • Multiplying BCBC first and then AA, A(BC)A(BC) takes 3×2×6+5×3×6=36+90=1263 \times 2 \times 6 + 5 \times 3 \times 6 = 36 + 90 = 126 multiplications.

The same product can need different numbers of multiplications depending on the order.

Given the sizes of NN matrices, write a program that finds the minimum number of multiplications needed to multiply all of them. The order of the input matrices must not be changed.

Input

The first line contains the number of matrices NN (1≤N≤20,0001 \le N \le 20{,}000).

From the second line on, each of the next NN lines contains the size rr and cc of a matrix. (1≤r,c≤10,0001 \le r, c \le 10{,}000)

Only sizes that can be multiplied in order are given.

Output

Print the minimum number of multiplications needed to multiply the input matrices on the first line. The answer is a positive integer no greater than 263−12^{63}-1. Even the worst order of operations uses no more than 263−12^{63}-1 multiplications.

Examples1

  1. Example 1

    Input
    3
    5 3
    3 2
    2 6
    
    Expected output
    90