codeforces-148E Porcelain

来源:互联网 发布:2017大数据发展报告 编辑:程序博客网 时间:2024/05/29 07:48

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.

ExamplesInput

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.

思路:
处理sum[i][j]数组表示,第i层砸j个花瓶能获取的最大价值。
然后考虑用每层体积为k,价值为sum[i][k]的物品,去装体积为m的背包。
强势构造成01背包。
此处注意,对每层来说,只能去1个数据。所以最后背包求解,额外加上一层循环。

#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdlib>#include <vector>#include <cstdio>#include <bitset>#include <string>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <list>#include <map>#include <set>using namespace std;int num[110][110];int a[110];int sum[110][110];int dp[110][11000];int main (){    memset(num,0,sizeof(num));    memset(sum,0,sizeof(sum));    memset(dp,0,sizeof(dp));    memset(a,0,sizeof(a));    int n,m;    scanf ("%d%d",&n,&m);    for(int i=1;i<=n;i++){        scanf ("%d",&a[i]);        for(int j=1;j<=a[i];j++){            int x;            scanf ("%d",&x);            num[i][j]=num[i][j-1]+x;        }    }    for(int i=1;i<=n;i++){//to get sum[i][k];        for(int j=1;j<=a[i];j++){            int l=a[i],r=j;//( ,]            for(int t=0;t<=j;t++){                sum[i][j]=max(sum[i][j],num[i][r]+num[i][a[i]]-num[i][l]);                l--,r--;            }        }    }    for(int i=1;i<=n;i++){        for(int j=m;j>=0;j--){            for(int k=0;k<=a[i]&&k<=j;k++){                dp[i][j]=max(dp[i][j],dp[i-1][j-k]+sum[i][k]);            }        }    }    printf("%d\n",dp[n][m]);    return 0;}