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
 
  1. //2017-08-03
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int N = ;
  10. const int inf = 0x3f3f3f3f;
  11. int n, a[N], dp[N][N];//dp[l][r]表示把区间l-r合并为回文的最小代价
  12. long long v[N], sum[N];
  13.  
  14. int dfs(int l, int r){
  15. if(l >= r)return dp[l][r] = ;
  16. if(dp[l][r] != -)return dp[l][r];//记忆化搜索
  17. int i = l;
  18. dp[l][r] = v[r-l+];
  19. for(int j = r; j >= l; j--){
  20. while((sum[i]-sum[l-]) < (sum[r]-sum[j-]) && i < j){
  21. i++;
  22. }
  23. if(j == i)break;
  24. if((sum[i]-sum[l-]) == (sum[r]-sum[j-])){//划分子区间,需保证区间左端所有数之和与区间右端所有数之和相等。
  25. int tmp = dfs(i+, j-)+v[i-l+]+v[r-j+];
  26. dp[l][r] = dp[l][r] < tmp ? dp[l][r] : tmp;
  27. }
  28. }
  29. return dp[l][r];
  30. }
  31.  
  32. int main(){
  33. while(scanf("%d", &n)!=EOF && n){
  34. sum[] = ;
  35. for(int i = ; i <= n; i++){
  36. scanf("%d", &a[i]);
  37. sum[i] = sum[i-]+a[i];
  38. }
  39. for(int i = ; i <= n; i++)
  40. scanf("%lld", &v[i]);
  41. memset(dp, -, sizeof(dp));
  42. int ans = dfs(, n);
  43. printf("%d\n", ans);
  44. }
  45.  
  46. return ;
  47. }

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. LOJ#3048. 「十二省联考 2019」异或粽子(trie树+堆)

    题面 传送门 题解 我们先把它给前缀异或和一下,然后就是要求前\(k\)大的\(a_i\oplus a_j\).把\(k\)乘上个\(2\),变成前\(2k\)大的\(a_i\oplus a_j\), ...

  2. 跟着刚哥学Redis

    NoSQL 简介 NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL".是对不同于传统的关系型数据库的数据库管理系统的统称.它泛指非关系型的数据库.随着 ...

  3. GoLang学习控制语句之for

    for结构简介 Go语言只有for循环这一种循环结构,Go语言中的for循环语句的三个部分不需要用括号括起来,但循环体必须用 { } 括起来.基本的for循环包含三个由分号分开的组成部分: 初始化语句 ...

  4. 小记 ArchLinux 下 Typora 无法输入中文

    今天准备写一篇 Linux 下的打印机文章,打开 Typora 时我发现不管我怎么设置都无法输入中文. pacman -R typora pacman -S typora 重新安装是无效的,我突然想起 ...

  5. C#导出Excel文件Firefox中文件名乱码

    首先说明下:我的解决方法不一定适用于其他遇到该问题的人,因为情况多种多样,适合我的方法不一定适合别人,就像我在遇到问题时查到别人的解决方案放到我的代码里却不管用,所以这个方法仅供参考 这两天做了一个导 ...

  6. C++(初学讲解):判断倍数

    问题描述输入一个整数,如果是5的倍数,那么输出倍数的值,否则输出NO. 输入描述一个整数. 输出描述输出倍数的值或者NO. 输入示例15 输出示例3 #include <iostream> ...

  7. alembic教程

    安装 pip install alembic 步骤 1.初始化 alembic 仓库 在终端中, cd 到你的项目目录中,然后执行命令 alembic init alembic ,创建一个名叫 ale ...

  8. StreamSets学习系列之启动StreamSets时出现Caused by: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "test.to.ensure.security.is.configured.correctly" "read")错误的解决办法

    不多说,直接上干货! 问题详情 [hadoop@master streamsets-datacollector-]$ ./bin/streamsets dc Java 1.8 detected; ad ...

  9. getFields和getDeclaredFields

    getFields()获得某个类的所有的公共(public)的字段,包括父类. getDeclaredFields()获得某个类的所有申明的字段,即包括public.private和proteced, ...

  10. 安装Windows 8.1过程中出现的各种问题(无损从MBR转GPT磁盘、不能定位已有分区)

    这个周末就安装了个系统,本以为一个小时就能搞定,没想到花费了将近一天. 我的机子是6G内存.500G硬盘,原装系统是Windows 7,现在想换成Windows 8.1,于是下载了64位的Window ...