Educational Codeforces Round 21E

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 ≤ 1000001 ≤ 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 ≤ 31 ≤ 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
题意:给你一个背包 装体积为wi 价值为vi的物品 ,求能装的价值最大

初看是一个背包问题,但是由于n很大,m也很大,用背包问题求解肯定会超时超内存,注意到 wi 只能为 1 2 3,所以可以通过枚举一个,用log的算法判断剩余形成的最大值

这里考虑枚举 3 的个数, 然后 三分2的个数, 因为知道2 的个数后,1的个数就能马上知道 。而2的个数和是一个单峰函数(可以证明的),最后复杂度是O(nlog)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#define maxn 400000
using namespace std;
typedef long long ll;
ll w1[maxn],w2[maxn],w3[maxn];
ll dp1[maxn],dp2[maxn],dp3[maxn];
ll len1=0,len2=0,len3=0;
ll cmp(ll x,ll y){
    return x>y;
}
ll init(ll m){
    dp1[0]=0;
    for(ll i = 1 ; i<=m ; i++){
        dp1[i]+=dp1[i-1]+w1[i-1];
    }
    dp2[0]=0;
    for(ll i = 1 ; i*2<=m ; i++){
        dp2[(i*2)]+=dp2[(i-1)*2]+w2[i-1];
        dp2[i*2-1]=dp2[(i-1)*2];
    }
    dp3[0]=0;
    for(ll i = 1 ; i*3<=m ; i++){
        dp3[i*3]+=dp3[(i-1)*3]+w3[i-1];
        dp3[i*3-1]=dp3[(i-1)*3];
        dp3[i*3-2]=dp3[(i-1)*3];
    }
    for(int i = 0 ; i<=m ; i++){
        //printf("%I64d %I64d %I64d\n",dp1[i],dp2[i],dp3[i]);
    }
}
ll f(ll x,ll m){
    return dp2[x*2]+dp1[m-x*2];
}
ll fun(ll m){
    ll sum=0;
    ll l=0;
    ll r=m/2;
    if(m==0)
        return 0;
    while(l < r-1)
    {
        int mid  = (l+r)/2;
        int mmid = (mid+r)/2;
        if( f(mid,m) > f(mmid,m) )
            r = mmid;
        else
            l = mid;
    }
    return max(max(max(f(l,m),f(r,m)),f(0,m)),f(m/2,m));
}
int main(){
    ll n , m ;
    scanf("%I64d%I64d",&n,&m);
    for(ll i = 0 ; i<n ; i++){
        ll x,y;
        scanf("%I64d %I64d",&x,&y);
        if(x==1ll){
            w1[len1++]=y;
        }
        if(x==2ll){
            w2[len2++]=y;
        }
        if(x==3ll){
            w3[len3++]=y;
        }
    }
    sort(w1,w1+len1,cmp);
    sort(w2,w2+len2,cmp);
    sort(w3,w3+len3,cmp);
    init(m);
    ll ans=0;
    for(ll i = 0 ; i<=len3&&i*3<=m ; i++){
        ll sum=dp3[i*3];
        sum+=fun(m-i*3);
        ans=max(sum,ans);
    }
    printf("%I64d\n",ans);
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值