Codeforces 808E Selling Souvenirs【思维+Dp】

351 篇文章 2 订阅
22 篇文章 1 订阅

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 weight wi and cost ci. 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 than m, and total cost is maximum possible.

Help Petya to determine maximum possible total cost.

Input

The first line contains two integers n and m (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 and ci (1 ≤ wi ≤ 3, 1 ≤ ci ≤ 109) — the weight and the cost of ith souvenir.

Output

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

Examples
Input
1 1
2 1
Output
0
Input
2 2
1 3
2 2
Output
3
Input
4 3
3 10
2 7
2 8
1 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 __int64
struct 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);
    }
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值