codeforces 148E Porcelain
2016-07-31 19:08
337 查看
E. Porcelain
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 of m shrieks can inflict on the collection of porcelain.
Input
The first line of input data contains two integers n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 10000). The next n lines contain the values of the items on the shelves: the first number gives the number of items on this shelf (an integer between 1 and 100, inclusive), followed by the values of the items (integers between 1 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.
Examples
input
2 3 3 3 7 2 3 4 1 5
output
15
input
1 3 4 4 3 1 2
output
9
Note
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<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct Shelf { int a[105],pre[105],suf[105]; int num; }; Shelf s[105]; int d[105][105],dp[10005]; int main() { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&s[i].num); for(int j=1;j<=s[i].num;j++) scanf("%d",&s[i].a[j]); for(int j=1;j<=s[i].num;j++) s[i].pre[j]=s[i].pre[j-1]+s[i].a[j]; for(int j=s[i].num;j>=1;j--) s[i].suf[j]=s[i].suf[j+1]+s[i].a[j]; } for(int i=1;i<=n;i++) for(int j=0;j<=s[i].num;j++) for(int k=s[i].num+1;k>j;k--) { int& t=d[i][j+s[i].num-k+1]; t=max(t,s[i].pre[j]+s[i].suf[k]); } for(int i=1;i<=n;i++) for(int j=m;j>=0;j--) for(int k=0;k<=s[i].num&&k<=j;k++) dp[j]=max(dp[j],dp[j-k]+d[i][k]); printf("%d\n",dp[m]); return 0; }
相关文章推荐
- codeforces-148E Porcelain
- CodeForces 160B Unlucky Ticket
- CodeForces 618A Slime Combining
- codeforces 161D Distance in Tree 树形dp
- Codeforces 635C XOR Equation【数学姿势】
- CodeForces - 834B
- CodeForces 907F Power Tower
- CodeForces 348C Subset Sums nsqrtn的姿势。。
- Codeforces 404D Minesweeper 1D
- codeforces-702A-Maximum Increase
- CodeForces 740A Alyona and copybooks
- CodeForces - 803C 数学贪心
- codeforces 761 B
- Codeforces 492D - Vanya and Computer Game (二分)
- codeforces 624C Graph and String
- CodeForces 94D End of Exams (贪心)
- codeforces 397div2 D Artsem and Saunders
- CodeForces - 831A Unimodal Array
- CodeForces 788 B.Weird journey(欧拉路径+组合数学)
- codeforces 493D Vasya and Chess(博弈?)