1080: 矩阵链排序问题

内存限制:512 MB 时间限制:1.500 S
评测方式:文本比较 命题人:
提交:5 解决:4

题目描述

给定 lns="http://www.w3.org/1998/Math/MathML">n 个矩阵,已知第 lns="http://www.w3.org/1998/Math/MathML">i 个矩阵 lns="http://www.w3.org/1998/Math/MathML">Mi 的大小为 lns="http://www.w3.org/1998/Math/MathML">wi 行 lns="http://www.w3.org/1998/Math/MathML">wi+1 列,而我们并不关心其内容。我们考虑将其按照顺序相乘(称其为链乘积):

lns="http://www.w3.org/1998/Math/MathML" display="block">M=M1×M2××Mn

矩阵乘法并不满足交换律,但是其满足结合律,因此我们可以通过合理安排结合顺序,尽可能减少需要的运算次数。在此题中,我们定义将一个大小为 lns="http://www.w3.org/1998/Math/MathML">a×b 的矩阵乘以一个大小为 lns="http://www.w3.org/1998/Math/MathML">b×c 的矩阵需要 lns="http://www.w3.org/1998/Math/MathML">abc 次运算。

请你算出将题目所给的 lns="http://www.w3.org/1998/Math/MathML">n 个矩阵进行链乘积所需的最少运算数。为了方便起见,你不需要构造方案。

输入

输入的第一行为一个正整数 lns="http://www.w3.org/1998/Math/MathML">n,代表矩阵的数量。

接下来的一行包含 lns="http://www.w3.org/1998/Math/MathML">n+1 个正整数,其中第 lns="http://www.w3.org/1998/Math/MathML">i 个整数为 lns="http://www.w3.org/1998/Math/MathML">wi,含义参考题目描述。

输出

输出包含一个整数,代表最小运算次数。

样例输入 复制

3
5 3 2 6

样例输出 复制

90

提示

样例解释:样例告诉我们有 lns="http://www.w3.org/1998/Math/MathML">n=3 个矩阵,其大小分别是 lns="http://www.w3.org/1998/Math/MathML">5×3lns="http://www.w3.org/1998/Math/MathML">3×2 和 lns="http://www.w3.org/1998/Math/MathML">2×6。分别考虑两种乘法顺序:

  • 先将 lns="http://www.w3.org/1998/Math/MathML">M1 和 lns="http://www.w3.org/1998/Math/MathML">M2 相乘得到一个 lns="http://www.w3.org/1998/Math/MathML">5×2 的矩阵,然后和 lns="http://www.w3.org/1998/Math/MathML">M3 相乘,此时运算次数为 lns="http://www.w3.org/1998/Math/MathML">5×3×2+5×2×6=90
  • 先将 lns="http://www.w3.org/1998/Math/MathML">M2 和 lns="http://www.w3.org/1998/Math/MathML">M3 相乘得到一个 lns="http://www.w3.org/1998/Math/MathML">3×6 的矩阵,然后和 lns="http://www.w3.org/1998/Math/MathML">M1 相乘,此时运算次数为 lns="http://www.w3.org/1998/Math/MathML">3×2×6+5×3×6=126

本题要求运算次数最少,因此答案为 lns="http://www.w3.org/1998/Math/MathML">90


对所有的数据,lns="http://www.w3.org/1998/Math/MathML">1n2×106lns="http://www.w3.org/1998/Math/MathML">1w104。其中:

  • 对 lns="http://www.w3.org/1998/Math/MathML">30% 的数据,满足 lns="http://www.w3.org/1998/Math/MathML">n500
  • 对另外 lns="http://www.w3.org/1998/Math/MathML">30% 的数据,满足 lns="http://www.w3.org/1998/Math/MathML">n2×105

提逝:本题数据较氵(绝对不是因为STAFF 懒的弄), 暴力可打