codeforces Round 21 808E. Selling Souvenirs 【dp好题】

codeforces Round 21 808E. Selling Souvenirs 【dp好题】

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


题意: 分别有1,2,3三种重量的物品 价值为ci   问重量为w的情况下 价值最大化

          其实就是增大版本的01背包 

 题解:  依次计算出1 2 3 背包的最大值 

      只有w=1 的时候  直接排序  dp[i]=dp[i-1]+c;

     当有重量为2的时候  比较dp[i] 之中最小的两个1相加 是否小于w=2的情况 小于直接替换

     当有重量为3的时候   直接 遍历3的使用次数就可以了 


#include <stdio.h>
#include <bits/stdc++.h>
#define mod 1000000007
typedef long long ll;
using namespace std;
priority_queue<int> q[4];
int n,w;
ll dp[4][300003];
void fun1(){       //处理w=1 直接大到小排列上去 
	int k=q[1].size();
	for(int i=1;i<=k && i<=w;i++){
		dp[1][i]=dp[1][i-1]+q[1].top();q[1].pop();
	}
	for(int i=k+1;i<=w;i++) dp[1][i]=dp[1][i-1];
}
void fun2(){   
	int k=q[2].size();
	ll c,l=1,sum=0,t=0;
	for(int i=1;i<=k && i<=w;i++){
		c=q[2].top();q[2].pop();
		for(int j=l;j<=w;j++){   
			l=j;     //计算2的大小  如果2的大小 大于未使用过的两个1 则替换掉 
			if(j>=2*(t+1) && dp[1][j-t*2]+sum<dp[1][j-t*2-2]+sum+c){
				dp[2][j]=sum+c+dp[1][j-t*2-2];
				t++;sum+=c;break; 
			}else{
				dp[2][j]=sum+dp[1][j-t*2];  //2不大于最小的两个1的情况 
			}
		}
	}
	for(int j=l;j<=w;j++){
		dp[2][j]=sum+dp[1][j-t*2];
	}
}
void fun3(){  //处理w=3  最简单的了 
	ll sum=0,t=0;
	dp[3][w]=dp[2][w];
	while(q[3].size()){   //直接计算 w=3使用的次数 使用一次等于 dp[2][w-t]+sum
		t+=3;sum+=q[3].top();q[3].pop();
		if(t<=w){
			dp[3][w]=max(dp[3][w],dp[2][w-t]+sum);
		}
	}
}
int main(){
	cin>>n>>w;
	int ww,c;
	for(int i=0;i<n;i++){
		scanf("%d%d",&ww,&c);
		if(ww==1) q[1].push(c);
		else if(ww==2)q[2].push(c);
		else q[3].push(c);
	}
	fun1();fun2();fun3();
	cout<<dp[3][w];
    return 0;
} 




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值