Codeforces 148 E Porcelain dp

来源:互联网 发布:java工程师就业班 编辑:程序博客网 时间:2024/05/29 06:35

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 33 3 7 23 4 1 5
output
15
input
1 34 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.

题意:n排书,最多拿m本,问最大价值是多少,接下来n行,每行第一个数字为这行书的数量,接下来是每本书的价值。注意,只能从每行的最右端或最左端拿书。

题解:
dp问题。因为拿书有限制条件,所以我们需要先处理一下,w[I][[j]为在第I行取了j本书时的价值,有三种取法:1,一直从最左端取(前缀和);2,一直从后面取(后缀和);3,左右分别取(for循环讨论)。

dp[I][j]表示在第I行取了j本书后的价值,状态转移为:dp[I][j]=max(dp[i-1][j-k]+w[I][k],dp[I][j]);

代码:

#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <cmath>#include <queue>#include <stack>#include <algorithm>#define mem(a) memset(a, 0, sizeof(a))#define eps 1e-5#define M 100005#define inf 1000000007using namespace std;int n,m;int d[101],dd[101][101],qian[101][101],hou[101][101],w[101][101],dp[101][10010];int main(){    while(~scanf("%d%d",&n,&m))    {        mem(d);        mem(dd);        mem(qian);        mem(hou);        mem(w);        mem(dp);        for(int i=1;i<=n;i++)        {            scanf("%d",&d[i]);            for(int j=1;j<=d[i];j++)            {                scanf("%d",&dd[i][j]);                qian[i][j]=qian[i][j-1]+dd[i][j];//前缀和            }        }        for(int i=1;i<=n;i++)        {            int tem=1;            for(int j=d[i];j>=1;j--)            {                hou[i][tem]=hou[i][tem-1]+dd[i][j];//后缀和                tem++;            }        }        for(int i=1;i<=n;i++)        {            for(int j=1;j<=d[i];j++)            {                w[i][j]=max(qian[i][j],hou[i][j]);                for(int k=1;k<=j;k++)                {                    w[i][j]=max(qian[i][k]+hou[i][j-k],w[i][j]);//三种取书方式                }            }        }        for(int i=1;i<=n;i++)        {            for(int j=0;j<=m;j++)            {                for(int k=0;k<=d[i]&&k<=j;k++)                {                dp[i][j]=max(dp[i][j],dp[i-1][j-k]+w[i][k]);                }            }        }        printf("%d\n",dp[n][m]);    }    }

0 0
原创粉丝点击