codeforces 148E Porcelain

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 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.

#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;
}
时间: 2024-08-06 19:47:00

codeforces 148E Porcelain的相关文章

Codeforces 148E Porcelain (预处理+多重背包)

E. Porcelain time limit per test:3 seconds memory limit per test:256 megabytes During her tantrums the princess usually smashes some collectable porcelain. Every furious shriek is accompanied with one item smashed. The collection of porcelain is arra

CodeForces 148E Porcelain dp+背包(水

题目链接:点击打开链接 题意: 给定一个n层书架,一共取m本书. 下面n行给出每层书的价值. 每次可以取任意一层的最左端或最右端的一本书. 问能获得的最大价值. 思路: 1.显然是先求出对于每层任取任意本书能获得的最大价值. 2.然后背包一下. 1: 对于一层书任意j本,那么一定是从左端取k本,右端取 j-k本,求个前缀和然后枚举 j和k即可.每层n^2的dp 2: 分组背包. import java.io.BufferedReader; import java.io.InputStreamRe

Codeforces 148E. Porcelain【多重背包】

题目大意: 有一个公主一生气就喜欢摔东西.现在有很多个柜子,每个柜子里面装着很多物品,公主每次摔东西只能随机的选择一个柜子,拿出最左边或者最右边的一个物品摔碎,给出公主最多生气的次数,求生完气之后,公主摔掉物品的价值的最大总和. 做法: 对于每个柜子来说,从左边拿和从右边拿是不一样的,假设最佳方案中,在第i个柜子中需要拿Ki个物品,那么拿着Ki个物品的最大价值我们是可以确定的.一:全从左边拿,二:全从右边拿,三,左边那一部分,右边拿一部分.最后,我们可以算出w[i][k](在第i个柜子中拿k个物

Codeforces 148E Porcelain [预处理+dp背包]

给出n行数,每行的数目不定(不超过100个),只能从行的两端取数m次,求所取数的和最大值 n (1?≤?n?≤?100) andm (1?≤?m?≤?10000). 看似很难上手 我甚至想每次取一个,然后搜索,,,搜索分支有200个 如果我们知道每行取k个最多能取多少的话 那么 这个问题就变成了 如果我在前i-1行取了j次得到x的破坏值 那么我要再第i行取k次得到get[k]的破坏值 也就是前i行共取了k+j次得到了x+get[k]的破坏值 背包问题 暴力求解出每行取k个最大的破坏值即可(一个滑

codeforces 148E Aragorn&#39;s Story 背包DP

Aragorn's Story Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/148/E Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who

codeforces - 148E 题解

题目大意:一个公主有一个摆满瓷器的架子,她生气的时候就要打碎m个瓷器.这个架子有n层,每层的瓷器每次只能从最左边拿或者从最右边拿,问打碎的瓷器的最大价值. 题解:这是一个泛化物品+分组背包的DP,首先将每一层上拿出瓷器的方案作为一个物品,拿出瓷器的方案的代价是瓷器数量,价值是这一层上所有方案中最大的价值. 首先为了计算方案的时候可以快速计算,我们在读入的时候就计算出前缀和sum,然后在计算方案的时候,每一层上打碎的数量对应一个物品,然后枚举总数量分配到两侧打碎的价格,得到最大价值.最后使用分组背

寒假训练3解题报告 CodeForces #148

CodeForces 148B 一道简单模拟,判断龙能够抓到公主几次,如果公主和龙同时到达公主的城堡,不算龙抓住她,因为路程除以速度可能会产生浮点数,所以这里考虑一下精度问题 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <iomanip> 5 #include <algorithm> 6 using namespace std; 7 #defi

Codeforces Round #105 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/148 比较简单的一场,最长的一题也才写了30行多一点 A. Insomnia cure time limit per test:2 seconds memory limit per test:256 megabytes ?One dragon. Two dragon. Three dragon?, - the princess was counting. She had trouble falling asleep, and

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多