E. PorcelainTime limit per test:3 secondsmemory 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 arranged neatly onN 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 ofm shrieks can inflict on the collection of Porcel Ain.
Input
The first line of input data contains the integersn (1?≤? N? ≤?100) andm (1?≤? M.≤?10000). The nextn lines contain the values of the items on the shelves:the first number gives the number of items on thi s shelf (an integer between1 andA, inclusive), followed by the values of the items (integers betwe En1 and, 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 are guaranteed to being at leastm.
Output
Output the maximal total value of a tantrum of m shrieks.
Sample Test (s)Input
2 33 3 7 23 4 1 5
Output
15
Input
1 34 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.
Title Link: http://codeforces.com/contest/148/problem/e
The main topic: to the N-level number, total to take m times, each time the number can be taken from any one layer of the leftmost or the right to take
Problem Analysis: Preprocessing all possible maximum values of J (J <= m) from both sides of each layer, followed by a common multi-knapsack problem
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int sum[105], ma[105], DP [10005];int Main () { int n, m; scanf ("%d%d", &n, &m); for (int i = 1; I <= n; i++) { int num; scanf ("%d", &num); for (int j = 1; j <= Num; j + +) { int tmp; scanf ("%d", &tmp); SUM[J] = sum[j-1] + tmp; Compute prefix and } //preprocessing memset (MA, 0, sizeof (MA)); for (int j = 0; J <= Num, J + +) for (int k = 0; k <= J; k++) ma[j] = max (Ma[j], sum[k] + sum[num]-sum[num + K-J]); printf ("\ n"); Multiple backpack for (int j = m; j >= 1; j--) for (int k = 1; k <= min (j, num), K + +) dp[j] = max (Dp[j], dp[j-k ] + ma[k]); } printf ("%d\n", Dp[m]);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 148E Porcelain (pretreatment + multi-pack)