程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Codeforces 148E Porcelain (預處理+多重背包)

Codeforces 148E Porcelain (預處理+多重背包)

編輯:關於C++

 

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 arranged neatly onn 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 ofm shrieks can inflict on the collection of porcelain.

Input

The first line of input data contains two 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 this shelf (an integer between1 and100, inclusive), followed by the values of the items (integers between1 and100, 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 leastm.

Output

Output the maximal total value of a tantrum of m shrieks.

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

 

 

題目大意:給n層數字,一共要取m次,每次取數字只能從任意一層的最左或最右取

 

 

題目分析:預處理出每層從兩邊取j (j <= m)個的所有可能的最大值,之後就是個普通的多重背包問題

 

 

#include 
#include 
#include 
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;  //計算前綴和
        }
        //預處理
        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(
);
        //多重背包
        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
, dp[m]);
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved