Discription
Far away from our world, there is a banana forest. And many lovely monkeys live there. One day, SDH(Song Da Hou), who is the king of banana forest, decides to hold a big party to celebrate Crazy Bananas Day. But the little monkeys don't know each other, so as the king, SDH must do something. 
Now there are n monkeys sitting in a circle, and each monkey has a making friends time. Also, each monkey has two neighbor. SDH wants to introduce them to each other, and the rules are: 
1.every time, he can only introduce one monkey and one of this monkey's neighbor. 
2.if he introduce A and B, then every monkey A already knows will know every monkey B already knows, and the total time for this introducing is the sum of the making friends time of all the monkeys A and B already knows; 
3.each little monkey knows himself; 
In order to begin the party and eat bananas as soon as possible, SDH want to know the mininal time he needs on introducing. 

Input

There is several test cases. In each case, the first line is n(1 ≤ n ≤ 1000), which is the number of monkeys. The next line contains n positive integers(less than 1000), means the making friends time(in order, the first one and the last one are neighbors). The input is end of file.

Output

For each case, you should print a line giving the mininal time SDH needs on introducing.

Sample Input

8
5 2 4 7 6 1 3 9

Sample Output

105

发现这是一个四边形不等式的模板题
#include<bits/stdc++.h>
#define ll long long
#define maxn 2005
using namespace std;
int f[maxn][maxn],n,m,w[maxn];
int pos[maxn][maxn],ans,l,r;
int main(){
while(scanf("%d",&n)==1){
ans=1<<30;
memset(f,0x3f,sizeof(f));
for(int i=1;i<=n;i++){
scanf("%d",w+i),w[i+n]=w[i];
f[i][i]=f[i+n][i+n]=0;
pos[i][i]=i,pos[i+n][i+n]=i+n;
}
m=n<<1;
for(int i=1;i<=m;i++) w[i]+=w[i-1]; for(int len=1;len<n;len++)
for(int i=1,j;(j=i+len)<=m;i++){
l=pos[i][j-1],r=pos[i+1][j];
for(int u=l;u<=r;u++) if(f[i][u]+f[u+1][j]<f[i][j]){
f[i][j]=f[i][u]+f[u+1][j];
pos[i][j]=u;
}
f[i][j]+=w[j]-w[i-1];
} for(int i=1;i<=n;i++) ans=min(ans,f[i][i+n-1]);
// for(int i=1;i<m;i++)
// for(int j=i;j<=m;j++) printf("%d %d:%d, %d \n",i,j,f[i][j],pos[i][j]); printf("%d\n",ans);
} return 0;
}

  

 

最新文章

  1. 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题
  2. 前端学习 第七弹: Javascript实现图片的延迟加载
  3. PgSQL dump 工具
  4. warning: #870-D: invalid multibyte character sequence
  5. SQL语句在查询分析器中可以执行,代码中不能执行
  6. BFGS方法
  7. Javascript基础系列之(五)条件语句(比较操作符)
  8. .NET(c#)new关键字的三种用法
  9. Blob未完成(待优化)
  10. 项目中logger、message错误信息的配置
  11. 如何在VBS脚本中显示“选择文件对话框”或“选择目录对话框”
  12. 引用reference作用域scope闭包closure上下文context用法
  13. Nginx配置反向代理
  14. 4步解决“Microsoft Office Professional Plus 2013在安装过程中出错”
  15. fflush 和 fsync 的区别
  16. Powermock2.0.0 详细 总结
  17. 牛客练习赛28 B数据结构(线段树)
  18. 分布式锁tair redis zookeeper,安全性
  19. BZOJ4818 LOJ2002 SDOI2017 序列计数 【矩阵快速幂优化DP】*
  20. 【51NOD】1201 整数划分

热门文章

  1. 设置vim 永久显示行号
  2. Python之路--序列化
  3. and和or运算
  4. hdu 6354
  5. ACM 广度优化搜索算法总结
  6. gpg: signing failed: secret key not available
  7. AD转换器的参数介绍
  8. HDU 4866 Shooting 扫描线 + 主席树
  9. clr(Windows 运行时和公共语言运行时)
  10. python - 字符串的内建函数