The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row.

The goal is to take cards in such order as to minimize the total number of scored points.

For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring 
10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000
If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be 
1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.

Input

The first line of the input contains the number of cards N (3 <= N <= 100). The second line contains N integers in the range from 1 to 100, separated by spaces.

Output

Output must contain a single integer - the minimal score.

Sample Input

6
10 1 50 50 20 5

Sample Output

3650
#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
const int N = + ;
const int INF = 0x3f3f3f3f;
int a[N], dp[N][N]; void Work(int n){
memset(dp, , sizeof(dp));
for(int len = ; len < n; len++){
for(int i = ; i <= n-; i++){
int j = i + len;
dp[i][j] = INF;
for(int k = i + ; k < j; k++)
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + a[i]*a[k]*a[j]);
}
}
printf("%d\n", dp[][n]);
}
int main(){
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
Work( n );
}

最新文章

  1. springMVC初始化绑定器
  2. C# async and await
  3. WPF MVVM初体验
  4. 【转】我应该直接学Swift还是Objective-C?
  5. php源码编译常见错误解决方案
  6. VBS基础篇 - Dictionary对象
  7. MFC Windows程序设计源代码免费下载
  8. Hibernate 集合映射 一对多多对一 inverse属性 + cascade级联属性 多对多 一对一 关系映射
  9. [译]Why do people write #!/usr/bin/env python on the first line of a Python script?
  10. Spark内存管理-UnifiedMemoryManager和StaticMemoryManager
  11. Mahout系列之-----相似度
  12. ORA-02030: can only select from fixed tables/views
  13. Redis数据结构以及应用场景
  14. JAVA-8大基本类型与包装类的例子(基础必备)
  15. OpenLDAP一登录系统就修改密码
  16. kafka partition(分区)与 group(转)
  17. 7.1Python异常处理
  18. 二、工作中常用的SQL优化
  19. Orchard模块开发全接触6:自定义用户注册
  20. java 静态方法上的泛型

热门文章

  1. C++ 没有合适的默认构造函数(无参数构造函数)
  2. jeesite安装时Perhaps you are running on a JRE rather than a JDK
  3. sh_03_程序计数
  4. JS深度判断两个数组对象字段相同
  5. sqli-labs(32)
  6. 大哥带我们的mysql注入 基于时间的盲注
  7. wannafly 挑战赛9 B 数一数(kmp)
  8. wannafly 练习赛11 E 求最值(平面最近点对)
  9. 问:在指定的JSON数据中(最外层是数组)根据指定条件拿到匹配到的结果
  10. 数据结构和算法(Java版)快速学习(线性表)