Codeforces 808E Selling Souvenirs【思维+Dp】

来源:互联网 发布:ubuntu llmp 编辑:程序博客网 时间:2024/05/17 23:22

E. Selling Souvenirs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working for and started to sell souvenirs at the market.

This morning, as usual, Petya will come to the market. Petya has n different souvenirs to sell; ith souvenir is characterised by its weightwi and costci. Petya knows that he might not be able to carry all the souvenirs to the market. So Petya wants to choose a subset of souvenirs such that its total weight is not greater thanm, and total cost is maximum possible.

Help Petya to determine maximum possible total cost.

Input

The first line contains two integers n andm (1 ≤ n ≤ 100000,1 ≤ m ≤ 300000) — the number of Petya's souvenirs and total weight that he can carry to the market.

Then n lines follow. ith line contains two integers wi andci (1 ≤ wi ≤ 3,1 ≤ ci ≤ 109) — the weight and the cost ofith souvenir.

Output

Print one number — maximum possible total cost of souvenirs that Petya can carry to the market.

Examples
Input
1 12 1
Output
0
Input
2 21 32 2
Output
3
Input
4 33 102 72 81 1
Output
10

题目大意:

和01背包相似,现在有N个物品,背包容量为M.每个物品有两个属性,分别是重量和价值。

特殊之处在于重量的数据范围是1~3.


思路:


1、观察到物品重量的范围是1~3.

那么我们不妨先将所有物品分成三类,然后对于每一类物品按照价值从大到小排序。

我们肯定贪心的想要取价值高的。


2、那么我们进行一个三元组Dp,设定Dp【i】表示容量为i的背包,其状态为:(重量为1的物品已经购买的数量,重量为2的物品已经购买的数量,最高价值);


那么我们对其Dp一下,那么对应再枚举购买重量为3的物品的个数即可。


3、不太明白为什么不能对三种物品都Dp搞一个四元组Dp..一直Wa 45.....................留给之后没事情做的时候去想把。


Ac代码:


#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>using namespace std;#define ll __int64struct node{    ll val;    ll x,y,z;}dp[300050];ll sum[300050];vector<ll>a[5];int main(){    ll n,m;    while(~scanf("%I64d%I64d",&n,&m))    {        memset(sum,0,sizeof(sum));        for(ll i=1;i<=4;i++)a[i].clear();        for(ll i=0;i<n;i++)        {            ll x,y;            scanf("%I64d%I64d",&x,&y);            a[x].push_back(y);        }        for(ll i=1;i<=3;i++)        {            sort(a[i].begin(),a[i].end());            reverse(a[i].begin(),a[i].end());            if(i==3)            {                for(int j=0;j<a[i].size();j++)                {                    if(j==0)sum[j]=a[i][j];                    else sum[j]=sum[j-1]+a[i][j];                }            }        }        memset(dp,0,sizeof(dp));        ll output=0;        for(ll i=1;i<=m;i++)        {            dp[i]=dp[i-1];            if(i>=1)            {                if(dp[i-1].x<a[1].size()&&dp[i-1].val+a[1][dp[i-1].x]>dp[i].val)                {                    dp[i].val=dp[i-1].val+a[1][dp[i-1].x];                    dp[i].x=dp[i-1].x+1;                    dp[i].y=dp[i-1].y;                    dp[i].z=dp[i-1].z;                }            }            if(i>=2)            {                if(dp[i-2].y<a[2].size()&&dp[i-2].val+a[2][dp[i-2].y]>dp[i].val)                {                    dp[i].val=dp[i-2].val+a[2][dp[i-2].y];                    dp[i].x=dp[i-2].x;                    dp[i].y=dp[i-2].y+1;                    dp[i].z=dp[i-2].z;                }            }            output=max(output,dp[i].val);        }        for(int j=0;j<a[3].size();j++)        {            if(m-(j+1)*3>=0)            output=max(output,dp[m-(j+1)*3].val+sum[j]);        }        printf("%I64d\n",output);    }}






原创粉丝点击