Another OCD Patient

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1757    Accepted Submission(s): 600

Problem Description

Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning, his children played with plasticene. They broke the plasticene into N pieces, and put them in a line. Each piece has a volume Vi. Since Xiaoji is an OCD patient, he can't stand with the disorder of the volume of the N pieces of plasticene. Now he wants to merge some successive pieces so that the volume in line is symmetrical! For example, (10, 20, 20, 10), (4,1,4) and (2) are symmetrical but (3,1,2), (3, 1, 1) and (1, 2, 1, 2) are not.

However, because Xiaoji's OCD is more and more serious, now he has a strange opinion that merging i successive pieces into one will cost ai. And he wants to achieve his goal with minimum cost. Can you help him?

By the way, if one piece is merged by Xiaoji, he would not use it to merge again. Don't ask why. You should know Xiaoji has an OCD.

 

Input

The input contains multiple test cases.

The first line of each case is an integer N (0 < N <= 5000), indicating the number of pieces in a line. The second line contains N integers Vi, volume of each piece (0 < Vi <=10^9). The third line contains N integers ai (0 < ai <=10000), and a1 is always 0.

The input is terminated by N = 0.

 

Output

Output one line containing the minimum cost of all operations Xiaoji needs.
 

Sample Input

5
6 2 8 7 1
0 5 2 10 20
0
 

Sample Output

10

Hint

In the sample, there is two ways to achieve Xiaoji's goal.
[6 2 8 7 1] -> [8 8 7 1] -> [8 8 8] will cost 5 + 5 = 10.
[6 2 8 7 1] -> [24] will cost 20.

 

Author

SYSU
 
 //2017-08-03
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std; const int N = ;
const int inf = 0x3f3f3f3f;
int n, a[N], dp[N][N];//dp[l][r]表示把区间l-r合并为回文的最小代价
long long v[N], sum[N]; int dfs(int l, int r){
if(l >= r)return dp[l][r] = ;
if(dp[l][r] != -)return dp[l][r];//记忆化搜索
int i = l;
dp[l][r] = v[r-l+];
for(int j = r; j >= l; j--){
while((sum[i]-sum[l-]) < (sum[r]-sum[j-]) && i < j){
i++;
}
if(j == i)break;
if((sum[i]-sum[l-]) == (sum[r]-sum[j-])){//划分子区间,需保证区间左端所有数之和与区间右端所有数之和相等。
int tmp = dfs(i+, j-)+v[i-l+]+v[r-j+];
dp[l][r] = dp[l][r] < tmp ? dp[l][r] : tmp;
}
}
return dp[l][r];
} int main(){
while(scanf("%d", &n)!=EOF && n){
sum[] = ;
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
sum[i] = sum[i-]+a[i];
}
for(int i = ; i <= n; i++)
scanf("%lld", &v[i]);
memset(dp, -, sizeof(dp));
int ans = dfs(, n);
printf("%d\n", ans);
} return ;
}

HDU4960(SummerTrainingDay03-F dp)的更多相关文章

  1. Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理

    https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...

  2. Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希

    https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...

  3. 【BZOJ4953】lydsy七月月赛 F DP

    [BZOJ4953]lydsy七月月赛 F 题面 题解:设f[i][j]表示第i个强度取为j时的最小误差.那么每次转移时,我们只计算j'和j之间的像素点带来的误差,于是有: $f[i][j]=min( ...

  4. hdu 4389 X mod f(x) 数位DP

    思路: 每次枚举数字和也就是取模的f(x),这样方便计算. 其他就是基本的数位Dp了. 代码如下: #include<iostream> #include<stdio.h> # ...

  5. HDU - 4389 X mod f(x)(数位dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...

  6. BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1375 题意: 给你n个数,你可以从中选任意多个,但不能不选.问你所选数字之和为f的倍数 ...

  7. F. Wi-Fi(线段树实现dp)

    题:http://codeforces.com/contest/1216/problem/F dp[i][0]:表示第i个位置不装的最小代价 dp[i][1]:表示第i个位置装的最小代价 T1的线段树 ...

  8. UVALive 6908---Electric Bike(DP或记录型深搜)

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  9. coderforces #384 D Chloe and pleasant prizes(DP)

    Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  10. HDU4003Find Metal Mineral[树形DP 分组背包]

    Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Other ...

随机推荐

  1. C++获取本机IP地址信息

    #include<winsock2.h> #include<iostream> #include<string> using namespace std; #pra ...

  2. GoLang学习之变量定义和初始化

    变量命名原则 go语言的变量名有字母数字和下划线组成,首字母不能为数字,但是字母不仅仅只限于英文字母,所有的UTF-8字符都是可以的. 变量声明和初始化方式 使用var关键字 var a int = ...

  3. sudoer命令各参数含义及设置

    对于普通用户sudo加权的时限制可以执行的命令 https://segmentfault.com/a/1190000007394449 需要修改/etc/sudoers 1. 字段说明如下 授权用户/ ...

  4. vue.js生命周期钩子函数及缓存

    在使用vue.js进行开发时,使用最多的就是created.mounted.activated. 由于有些情况下,我们需要复用某些组件,因此需要用到keep-alive. 当引入keep-alive时 ...

  5. 使用IST重新加入节点(5.7.20)

    IST不是SST用于节点重新加入吗?我们有解决方案! 鉴于上述痛点,我们将介绍 gcache.freeze_purge_at_seqno Percona XtraDB Cluster 5.7.20.这 ...

  6. ubuntu安转QTcreator出现The default mkspec symlink is broken

    QT Creator安装:https://blog.csdn.net/arackethis/article/details/42326967 QT SDK安装:https://blog.csdn.ne ...

  7. Windows下的Jdk 1.7*安装并配置(图文详解)

    不多说,直接上干货! 很多人很少去想,为什么在windows下,安装完Jdk的安装包之后,还需要去配置环境变量,只是知道要这么去做,没有想过为什么要去这么做? 答:由于java是平台无关的 ,安装jd ...

  8. 全网最详细的Sublime Text 3的设置字体及字体大小(图文详解)

    不多说,直接上干货! 前期博客 全网最详细的Windows里下载与安装Sublime Text *(图文详解) 全网最详细的Sublime Text 3的激活(图文详解) 你也许是如下的版本:   点 ...

  9. Word在转PDF的过程中如何创建标签快速方便阅读(图文详解)

    不多说,直接上干货! 选择如下 成功! 欢迎大家,加入我的微信公众号:大数据躺过的坑        人工智能躺过的坑       同时,大家可以关注我的个人博客:    http://www.cnbl ...

  10. mycat中间件--schema.xml配置文件详解

    schema.xml管理着MyCat的逻辑库.表.分片规则.DataNode以及DataSource.弄懂这些配置,是正确使用MyCat的前提. <?xml version="1.0& ...