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 is placed in a row, so that one can access is only the outermost items-the leftmost or the R Ightmost item, not the ones in the middle of the shelf. Once an item is taken, and the next item on this side of the shelf can be accessed (see example). Once an item was taken, it can ' t be returned to the shelves.
You is 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 P Orcelain.
Input
The first line of input data contains integers n (1≤ n ≤100) and m (1≤< C7>m ≤10000). The next n lines contain the values of the items on the shelves:the first number gives the number of items O n this shelf (a integer between 1 and inclusive), followed by the values of the items (integers between 1 and inclusive), in the order in which they appear on the shelf (the first number corresponds to the Leftmos T item, the last one-to the rightmost one). The total number of items are guaranteed to being at least m.
Output
Output the maximal total value of a tantrum of m shrieks.
Examplesinput
2 3
3 3 7 2
3 4 1 5
Output
15
Input
1 3
4 4 3) 1 2
Output
9
Note
The first case there is shelves, each with three items. To maximize the total value of the items chosen, one can take both 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 was only one shelf, so all three items be taken from It-two from the "left" side and one from the Right side.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;structshelf{inta[ the],pre[ the],suf[ the]; intnum;}; Shelf s[ the];intd[ the][ the],dp[10005];intMain () {intn,m; scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) {scanf ("%d",&s[i].num); for(intj=1; j<=s[i].num;j++) scanf ("%d",&S[i].a[j]); for(intj=1; j<=s[i].num;j++) S[i].pre[j]=s[i].pre[j-1]+S[i].a[j]; for(intj=s[i].num;j>=1; j--) S[i].suf[j]=s[i].suf[j+1]+S[i].a[j]; } for(intI=1; i<=n;i++) for(intj=0; j<=s[i].num;j++) for(intk=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(intI=1; i<=n;i++) for(intj=m;j>=0; j--) for(intk=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