Codeforces 148E. Porcelain,codeforces148e

來源:互聯網
上載者:User

Codeforces 148E. Porcelain,codeforces148e


預先處理每個架子上打壞n個的最大毀壞值,就轉化成經典的分組背包問題了.......

E. Porcelaintime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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.

Sample test(s)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.


import java.util.*;public class Porcelain {    static int n, m;    static int[] s = new int[110];    static int[][] sum = new int[110][110];    static int[][] dem = new int[110][110];    static int[][] dp = new int[110][11000];    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        n = in.nextInt();        m = in.nextInt();        for (int i = 1; i <= n; i++) {            s[i] = in.nextInt();            for (int j = 1; j <= s[i]; j++) {                int x = in.nextInt();                sum[i][j] = sum[i][j - 1] + x;            }        }        for (int i = 1; i <= n; i++) {            for (int j = 1; j <= s[i]; j++) {                int t = 0;                for (int left = 0; left <= j; left++) {                    int right = j - left;                    int temp = sum[i][left] + sum[i][s[i]] - sum[i][s[i] - right];                    t = Math.max(t, temp);                }                dem[i][j] = t;            }        }        for (int i = 1; i <= n; i++) {            for (int j = m; j >= 0; j--) {                for (int k = 0; k <= s[i] && k <= j; k++) {                    dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - k] + dem[i][k]);                }            }        }        System.out.println(dp[n][m]);    }}








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.