Under construction

Still being built. Things may change

Matrix Multiplication Order 4

Time limit1sMemory limit512 MB

Summary
Given N matrices whose row and column counts never increase, find the fewest multiplications needed to multiply them in their fixed order, with N up to 1,000,000.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy
Solved
No attempts yet

Problem

Multiplying an N×MN \times M matrix AA by an M×KM \times K matrix BB takes N×M×KN \times M \times K multiplications in total. The number of multiplications needed to multiply NN matrices depends 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×22 \times 2, and consider computing the product ABCABC.

  • If ABAB is computed first and then multiplied by CC, computing (AB)C(AB)C takes 5×3×2+5×2×2=30+20=505 \times 3 \times 2 + 5 \times 2 \times 2 = 30 + 20 = 50 multiplications.
  • If BCBC is computed first and then AA is multiplied by it, computing A(BC)A(BC) takes 3×2×2+5×3×2=12+30=423 \times 2 \times 2 + 5 \times 3 \times 2 = 12 + 30 = 42 multiplications.

The same product needs a different number 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 cannot be changed.

Input

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

From the second line to line N+1N+1, line i+1i+1 contains the size rir_i and cic_i of the ii-th matrix. (1≤ri,ci≤10,0001 \le r_i, c_i \le 10{,}000)

The input always contains sizes that can be multiplied in order.

The sizes of the given matrices are guaranteed to be monotonically non-increasing. That is, for 1≤i<j≤n1 \le i < j \le n, ri≥rjr_i \ge r_j and ci≥cjc_i \ge c_j.

Output

Print the minimum number of multiplications needed to multiply the given matrices on the first line. The answer is a natural number less than or equal to 263−12^{63}-1. Also, even when the matrices are multiplied in the worst order, the number of multiplications is less than or equal to 263−12^{63}-1.

Examples1

  1. Example 1

    Input
    3
    5 3
    3 2
    2 2
    
    Expected output
    42