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 ofith 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

  题目大意就是0-1背包问题,然后看着逆天的数据范围,O(nm)的算法怕是去卡评测机的。

  只能另想出路了。注意到每个物品的最大重量为3,突破口应该就在这儿。

  先按照物品的重量分类,排序(从大到小,因为同种重量选价值大不会更劣),求前缀和。

  考虑枚举重量为3的物品选择的物品的数量。考虑对剩下物品dp。不难证明容量+1后对策略的影响只有:

  • 加入一个重量为1的物品。
  • 拿走一个重量为1的物品加入一个重量为2的物品。

  (大概就是因为每次改变的物品重量和不会超过2,如果改变的物品重量和超过2,那么两边一定存在1个重量和为2的物品集合,把现在这个集合里的替换为新的集合中会更优,这样会矛盾)

  不过暴力记下每种重量的物品选择的数量也可以dp。讨论一下发现和这个操作是类似的。

  如果将两个物品拿来dp,然后最后枚举状态,计算第三种物品需要的量,再根据前缀和快速计算。如果用重量为1和2的物品来dp,经过一番考虑决定用f[i]表示当前总共装了质量为i的物品最大的价值和和两种物品各用的数量(对,没有看错,用的是一个结构体来存的)。

  为什么这么做是正确的?

  首先我们按照从大到小排序,如果说一个重量为2的物品比两个重量为1的物品优,那么对于这个状态以及这之前的状态,两个质量为1的物品都不会更优,并且会被这个重量为2的物品替换掉,也就是说最优的时候有确定的两种物品的数量,即使是两个重量为1的物品之和和一个重量为2的物品价值和相等,也不会影响。转移的时候我肯定希望能放的尽量大,所以转移的时候只需要枚举是装入下一个重量为1的物品还是重量为2的物品。

  最后计算一下答案,取max即可。

-->

Code

 /**
* Codeforces
* Problem#808E
* Accepted
* Time:31ms
* Memory:9400k
*/
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} #define LL long long typedef class Data {
public:
LL val;
int s1, s2; Data():val(), s1(), s2() { }
}Data; int n, m;
Data* f;
int cnt[] = {, , , };
int vs[][];
LL s[][]; inline void init() {
readInteger(n);
readInteger(m);
f = new Data[(const int)(m + )];
for(int i = , v, w; i <= n; i++) {
readInteger(w);
readInteger(v);
vs[w][++cnt[w]] = v;
}
} boolean cmpare (const int& a, const int& b) {
return a > b;
} LL res = ; inline void solve() {
for(int i = ; i <= ; i++) {
sort(vs[i] + , vs[i] + cnt[i] + , cmpare);
s[i][] = ;
for(int j = ; j <= cnt[i]; j++)
s[i][j] = s[i][j - ] + vs[i][j];
} for(int i = ; i <= m; i++) {
if(f[i - ].s1 < cnt[] && f[i].val < f[i - ].val + vs[][f[i - ].s1 + ]) {
f[i].val = f[i - ].val + vs[][f[i - ].s1 + ];
f[i].s1 = f[i - ].s1 + , f[i].s2 = f[i - ].s2;
}
if(i >= && f[i - ].s2 < cnt[] && f[i].val < f[i - ].val + vs[][f[i - ].s2 + ]) {
f[i].val = f[i - ].val + vs[][f[i - ].s2 + ];
f[i].s2 = f[i - ].s2 + , f[i].s1 = f[i - ].s1;
}
} for(int i = ; i <= m; i++)
if((m - f[i].s1 - f[i].s2 * ) / <= cnt[])
smax(res, f[i].val + s[][(m - f[i].s1 - f[i].s2 * ) / ]);
else
smax(res, f[i].val + s[][cnt[]]);
printf(Auto, res);
} int main() {
init();
solve();
return ;
}

Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心的更多相关文章

  1. Educational Codeforces Round 21 Problem D(Codeforces 808D)

    Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into t ...

  2. Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

    Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...

  3. Educational Codeforces Round 21 Problem A - C

    Problem A Lucky Year 题目传送门[here] 题目大意是说,只有一个数字非零的数是幸运的,给出一个数,求下一个幸运的数是多少. 这个幸运的数不是最高位的数字都是零,于是只跟最高位有 ...

  4. Educational Codeforces Round 21

    Educational Codeforces Round 21  A. Lucky Year 个位数直接输出\(1\) 否则,假设\(n\)十进制最高位的值为\(s\),答案就是\(s-(n\mod ...

  5. Educational Codeforces Round 32 Problem 888C - K-Dominant Character

    1) Link to the problem: http://codeforces.com/contest/888/problem/C 2) Description: You are given a ...

  6. Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串

    题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...

  7. Codeforces Round #524 (Div. 2) codeforces 1080A~1080F

    目录 codeforces1080A codeforces 1080B codeforces 1080C codeforces 1080D codeforces 1080E codeforces 10 ...

  8. Educational Codeforces Round 21 D.Array Division(二分)

    D. Array Division time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  9. Educational Codeforces Round 21(A.暴力,B.前缀和,C.贪心)

    A. Lucky Year time limit per test:1 second memory limit per test:256 megabytes input:standard input ...

随机推荐

  1. render, render_to_response, redirect,

    自django1.3开始:render()方法是render_to_response的一个崭新的快捷方式,前者会自动使用RequestContext.而后者必须coding出来,这是最明显的区别,当然 ...

  2. html中label及加上属性for之后的用法

    定义和用法 <label> 标签为 input 元素定义标签(label). label 元素不会向用户呈现任何特殊的样式.不过,它为鼠标用户改善了可用性,因为如果用户点击 label 元 ...

  3. 学习计划 mysql 主从复制

    网上资料繁多,需要跳多少坑才能实现,跳跳就知道了. -- 主从复制 如题:主数据库进行的操作,从数据库进行备份. -- 原理 有关于这方面的原理网上也是一搜一大片,去看看吧.肯定没错. 这里简单说一下 ...

  4. mybatis中大于等于、小于等于的写法

    在xml格式中,常常会遇到xml解析sql时候出错,这个时候需要用其他符号来表示.在mybatis中会遇到,需要做如下的转换:

  5. 借用HTML5 插入视频。音频

    HTML5 规定了一种通过 video 元素来包含视频的标准方法. 插入视频 <video width="320" height="240" contro ...

  6. 【Loadrunner】【浙江移动项目手写代码】代码备份

    vuser_init(){        lr_start_transaction("login"); web_url("10.78.224.136:8080" ...

  7. 迁移到 Linux:使用 sudo | Linux 中国

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/F8qG7f9YD02Pe/article/details/80976600 https://mmbi ...

  8. lua源代码学习(一)lua的c api外围实现

    工作后,整个人已经比較松懈了.尽管一直在看lua的源代码.可是一直是比較零碎的时间,没有系统的整理,所以还是收获不多.由于近期工作也不是非常忙了,就想整理下lua的源代码学习的笔记.加深下印象,并分享 ...

  9. Celery配置Redis数据库保存Return结果状态

    使用windows版本 1.于GitHUB下载https://github.com/ServiceStack/redis-windows Window版本,到路径: 2. 运行路径下:D:\redis ...

  10. Unity 小知识

    1.如何使物体颜色成2D效果 点开材质球的Shader属性,选择Unlit/Color 2.画布属性中的UI Scale Mode中可以设为 S W S S,这样屏幕大小缩放时,画布内的物体大小随之缩 ...