#P1753. 矩阵链排序问题

    ID: 11659 远端评测题 1500ms 1024MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP图论区间 DP图论建模可并堆Ad-hoc

矩阵链排序问题

Problem Description

Given nn matrices, the ii-th matrix MiM_i has size wiw_i rows and wi+1w_{i+1} columns, and we do not care about their contents. We consider multiplying them in order (called the chain product):

M=M1×M2××MnM = M_1 \times M_2 \times \cdots \times M_n

Matrix multiplication is not commutative, but it is associative. Therefore, by choosing a good order of association, we can reduce the number of operations as much as possible. In this problem, we define that multiplying a matrix of size a×ba \times b by a matrix of size b×cb \times c requires abcabc operations.

Please compute the minimum number of operations needed to compute the chain product of the given nn matrices. For convenience, you do not need to construct the optimal order.

Input Format

The first line contains a positive integer nn, representing the number of matrices.

The next line contains n+1n+1 positive integers. The ii-th integer is wiw_i, with the meaning described above.

Output Format

Output one integer, representing the minimum number of operations.

3
5 3 2 6
90

Hint

Sample explanation: The sample tells us that there are n=3n = 3 matrices, with sizes 5×35 \times 3, 3×23 \times 2, and 2×62 \times 6. Consider the two possible multiplication orders:

  • Multiply M1M_1 and M2M_2 first to get a 5×25 \times 2 matrix, then multiply it by M3M_3. The number of operations is 5×3×2+5×2×6=905 \times 3 \times 2 + 5 \times 2 \times 6 = 90.
  • Multiply M2M_2 and M3M_3 first to get a 3×63 \times 6 matrix, then multiply it by M1M_1. The number of operations is 3×2×6+5×3×6=1263 \times 2 \times 6 + 5 \times 3 \times 6 = 126.

This problem asks for the minimum number of operations, so the answer is 9090.


Constraints: For all testdata, 1n2×1061 \leq n \leq 2 \times 10^6, 1w1041 \leq w \leq 10^4. Specifically:

  • For 30%30\% of the testdata, n500n \leq 500.
  • For another 30%30\% of the testdata, n2×105n \leq 2 \times 10^5.

Input Format

{{formatI}}

Output Format

{{formatO}}

Hint

{{hint}}

Translated by ChatGPT 5