Porcelain codeforce (背包)
来源:互联网 发布:php集成环境哪个好 编辑:程序博客网 时间:2024/06/02 15:42
Description
During her tantrums the princess usually smashes some collectable porcelain. Every furious shriek is accompanied with one item smashed.
The collection of porcelain is arranged neatly on n shelves. Within each shelf the items are placed in one row, so that one can access only the outermost items — the leftmost or the rightmost item, not the ones in the middle of the shelf. Once an item is taken, the next item on that side of the shelf can be accessed (see example). Once an item is taken, it can't be returned to the shelves.
You are given the values of all items. Your task is to find the maximal damage the princess' tantrum ofm shrieks can inflict on the collection of porcelain.
Input
The first line of input data contains two integers n (1 ≤ n ≤ 100) andm (1 ≤ m ≤ 10000). The nextn lines contain the values of the items on the shelves: the first number gives the number of items on this shelf (an integer between1 and 100, inclusive), followed by the values of the items (integers between1 and 100, inclusive), in the order in which they appear on the shelf (the first number corresponds to the leftmost item, the last one — to the rightmost one). The total number of items is guaranteed to be at least m.
Output
Output the maximal total value of a tantrum of m shrieks.
Sample Input
2 33 3 7 23 4 1 5
15
1 34 4 3 1 2
9
Hint
In the first case there are two shelves, each with three items. To maximize the total value of the items chosen, one can take two items from the left side of the first shelf and one item from the right side of the second shelf.
In the second case there is only one shelf, so all three items are taken from it — two from the left side and one from the right side.
#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>using namespace std;int dp[10010];int val[110];int a[110];int main(){int n, m;while(scanf("%d %d", &n, &m) != EOF){memset(dp, 0, sizeof(dp));for(int i = 1; i <= n; i++){int k;scanf("%d", &k);for(int j = 1; j <= k; j++)scanf("%d", &a[j]);a[k+1] = 0;int sum1 = 0;memset(val, 0, sizeof(val));for(int j = 0; j <= k; j++){sum1 += a[j];int sum2 = 0;for(int t = k+1; t >= 1; t--){sum2 += a[t];if(j >= t)continue;val[j+k+1-t] = max(val[j+k+1-t], sum1+sum2); } } for(int j = m; j >= 0; j--){for(int t = 1; t <= k; t++){if(j >= t)dp[j] = max(dp[j], dp[j-t]+val[t]);}}}printf("%d\n", dp[m]);} return 0; }
- Porcelain codeforce (背包)
- cf105 E. Porcelain 背包dp
- codeforces 148E Porcelain 多重背包
- Codeforces 148E Porcelain [预处理+dp背包]
- Codeforces 148E. Porcelain【多重背包】
- CodeForces 148E Porcelain dp+背包(水
- codeforces 148E Porcelain(DP, 分组背包)
- CodeForces 148E Porcelain(dp+背包)
- Codeforces Round #105 (Div. 2)--E. Porcelain 01背包
- Codeforces 148E Porcelain (预处理+多重背包)
- Codeforce 106C(多重背包)
- Codeforce 632E(背包变形)
- Codeforce 742D(01背包)
- Codeforces Round #105 (Div. 2) E. Porcelain —— DP(背包问题)
- Codeforce 730J(贪心+01背包)
- codeforce
- codeforce
- Codeforce
- 如何成为运维专家
- 串口调试软件3.0
- 杭电_1050_Moving Tables
- linux 系统调用system call
- onActivityResult在android中的用法
- Porcelain codeforce (背包)
- 扩展UIImage
- 算法——排序之插入排序
- MySQL索引的Index method中btree和hash的区别
- JS时间倒计时
- Jquery无刷新,解析json
- phpstorm和firefox协同调试web(html)+php语句(或web(html)跳转到php语句)
- linux c/c++ 帮助 help
- PHP中$_SERVER的详细参数与说明