Harry Potter has n mixtures in front of him, arranged in a row. Each mixture has one of 100 different colors (colors have numbers from 0 to 99).

He wants to mix all these mixtures together. At each step, he is going to take two mixtures that stand next to each other and mix them together, and put the resulting mixture in their place.

When mixing two mixtures of colors a and b, the resulting mixture will have the color (a+b) mod 100.

Also, there will be some smoke in the process. The amount of smoke generated when mixing two mixtures of colors a and b is a*b.

Find out what is the minimum amount of smoke that Harry can get when mixing all the mixtures together.

Input

There will be a number of test cases in the input.

The first line of each test case will contain n, the number of mixtures, 1 <= n <= 100.

The second line will contain n integers between 0 and 99 - the initial colors of the mixtures.

Output

For each test case, output the minimum amount of smoke.

Example

Input:
2
18 19
3
40 60 20 Output:
342
2400

In the second test case, there are two possibilities:

  • first mix 40 and 60 (smoke: 2400), getting 0, then mix 0 and 20 (smoke: 0); total amount of smoke is 2400
  • first mix 60 and 20 (smoke: 1200), getting 80, then mix 40 and 80 (smoke: 3200); total amount of smoke is 4400

The first scenario is a much better way to proceed.

题解

这是一道傻逼区间dp,就没啥难度,预处理一下区间和即可

详见代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#define MAX_N 105
#define INF 0x3f3f3f3f
using namespace std; int dp[MAX_N][MAX_N];
int sum[MAX_N]; int n;
int a[MAX_N]; int main() {
cin.sync_with_stdio(false);
while (cin >> n) {
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
dp[i][j] = INF;
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; i++) {
cin >> a[i];
dp[i][i] = 0;
sum[i] = sum[i - 1] + a[i];
}
for (int i = 1; i <= n; i++)
for (int j = 1; j + i <= n; j++)
for (int k = j; k < j + i; k++)
dp[j][j + i] = min(dp[j][j + i], dp[j][k] + dp[k + 1][j + i] +
((sum[k] - sum[j - 1]) % 100) *
((sum[j + i] - sum[k]) % 100));
cout << dp[1][n] << endl;
}
return 0;
}

  

SPOJ MIXTURES 区间dp的更多相关文章

  1. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  2. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  3. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  4. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  5. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. BZOJ 1260&UVa 4394 区间DP

    题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...

  8. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

随机推荐

  1. C#值类型和引用类型与Equals方法

    1. C#的值类型和引用类型 C#的对象里面有两种类型,一个是引用类型,一个是值类型,值类型和引用类型的具体分类可以看下面的分类.   在C#中,不管是引用类型还是值类型,他们都隐式继承Object类 ...

  2. Bootstrap历练实例:禁用的按钮

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  3. tableview 删除cell

    正如在以前的帖子说,但是我在转到故事版(StoryBoard)教程之前,我有另外一个问题来回答. 我如何从UITableView删除一行呢? 当人们构建简单的表视图引用程序后,这是另一个常见的​​问题 ...

  4. JavaScript设计模式基础之闭包(终)

    对于前端程序员来说闭包还是比较难以理解的, 闭包的形成与变量的作用域以及变量的生产周期密切相关,所以要先弄懂变量的作用域和生存周期. 1.变量作用域 变量的作用域,就是指变量的有效范围,通常我们指的作 ...

  5. 【图论 动态规划拆点】luoguP3953 逛公园

    经典的动态规划拆点问题. 题目描述 策策同学特别喜欢逛公园.公园可以看成一张 NN 个点 MM 条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口, NN 号点是公园的出口,每条边有一个非负 ...

  6. 文件操作-dd

    Linux dd命令 用于读取.转换并输出数据. dd可从标准输入或文件中读取数据,根据指定的格式来转换数据,再输出到文件.设备或标准输出. 参数说明: if=文件名: 输入文件名,缺省为标准输入.即 ...

  7. CentOS6.5卸载openJDK和安装Sun JDK

    CentOS6.5卸载openJDK和安装Sun JDK 最近业务需要,新安装了CentOS6.5系统,在配置tomcat的时候,总是报错找不到jdk中的java.研究了半天,发现应该是openJDK ...

  8. 对于js运动中产生的问题

    1.不同的对象调用同一个定时器情况,则需要将定时器的名称定为该对象的一个属性来进行运用. 例: <!DOCTYPE html> <html lang="en"&g ...

  9. python插件,pycharm基本用法,markdown文本编写,jupyter notebook的基本操作汇总

    5.14自我总结 一.python插件插件相关技巧汇总 安装在cmd上运行 #比如安装 安装:wxpy模块(支持 Python 3.4-3.+ 以及 2.7 版本):pip3 install wxpy ...

  10. python计算机基础(一)

    什么是编程语言? 跟计算机交流的语言 什么是编程? 编程就是写代码,让计算机能够听懂的语言 为什么要编程? 让计算机为我们做事,取代人 计算机5大组成分别有什么作用? CPU:控制,判断,配作用,内存 ...