# Codeforces 808E Selling Souvenirs（花费是倍数关系的背包）

E. Selling Souvenirs
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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

### 题目大意

就是一个物品个数和容量都是105$10^5$级别的01背包问题，不过物品的花费只有1,2,3$1, 2, 3$三种。

### 解题思路

如果只有花费成倍数的物品，我们可以把小的转化成大的，然后空缺用小的添来贪心解决。所以对于这道题我们可以贪心地使用DP的方式预处理出任意花费1和2组成的最大价值，然后枚举3的个数凑成题目要求的容量，更新答案即可。（预处理1和3，枚举2也可以）

### AC代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
#define LL long long

const int MAXM=300000+3;

struct Statue
{
int p1, p2;//花费为1，2的物品的使用个数
LL val;//当前最大价值
}dp[MAXM];

int N, M;
vector<int> save_val[5];

int main()
{
scanf("%d%d", &N, &M);
for(int i=0;i<N;++i)
{
int w, v;
scanf("%d%d", &w, &v);
save_val[w].push_back(v);
}
for(int i=1;i<=3;++i)
sort(save_val[i].begin(), save_val[i].end(), greater<int>());
for(int i=1;i<=M;++i)
{
dp[i]=dp[i-1];
if(dp[i-1].p1+1<=save_val[1].size() && dp[i-1].val+save_val[1][dp[i-1].p1]>dp[i].val)
{
dp[i].val=dp[i-1].val+save_val[1][dp[i-1].p1];
dp[i].p1=dp[i-1].p1+1;
dp[i].p2=dp[i-1].p2;
}
if(i>1 && dp[i-2].p2+1<=save_val[2].size() && dp[i-2].val+save_val[2][dp[i-2].p2]>dp[i].val)
{
dp[i].val=dp[i-2].val+save_val[2][dp[i-2].p2];
dp[i].p1=dp[i-2].p1;
dp[i].p2=dp[i-2].p2+1;
}
}
LL sum=0, ans=0;
for(int i=0;i<=save_val[3].size() && i*3<=M;++i)
{
ans=max(ans, sum+dp[M-i*3].val);
if(i<save_val[3].size())
sum+=save_val[3][i];
}
printf("%lld\n", ans);

return 0;
}