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

Copy
1 1
2 1
output

Copy
0
input

Copy
2 2
1 3
2 2
output

Copy
3
input

Copy
4 3
3 10
2 7
2 8
1 1
output

Copy
10

题意:一个物品个数和容量都是1e5级别的01背包问题,不过物品的花费只有1,2,3
思路:先把物品按照花费分为3类,1,2,3,然后排序成降序,先以只取花费为1和2的物品进行DP,
然后再用花费为3的物品去尝试更换掉1和2的物品,使总价格最大。
还有巨巨有三分的写法,没有看懂= = ,显然太菜了。
细节见代码。
思路观自大佬的博客:https://blog.csdn.net/yasola/article/details/78222203
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,m;
std::vector<int> v[];
struct node
{
int w1;
int w2;
ll value;
}dp[maxn];
int main()
{
gg(n);
gg(m);
int x,y;
repd(i,,n)
{
gg(x),gg(y);
v[x].pb(y); }
repd(i,,)
{
sort(all(v[i]),greater<int>());
}
repd(i,,m)
{
dp[i]=dp[i-];
if(dp[i-].w1<=sz(v[])-&&dp[i].value<dp[i-].value+v[][dp[i-].w1])
{
dp[i].value=dp[i-].value+v[][dp[i-].w1];
dp[i].w1++;
}
if(i>)
{
if(dp[i-].w2<=sz(v[])-&&dp[i].value<dp[i-].value+v[][dp[i-].w2])
{
dp[i].value=dp[i-].value+v[][dp[i-].w2];
dp[i].w2=dp[i-].w2+;
dp[i].w1=dp[i-].w1;
}
} }
ll sum=0ll;
ll ans=0ll;
for(int i=;i<=sz(v[])&&i*<=m;++i)
{
ans=max(ans,sum+dp[m-i*].value);
if(i<sz(v[]))
{
sum+=v[][i];
}
}
printf("%lld\n",ans );
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Selling Souvenirs CodeForces - 808E (分类排序后DP+贪心)的更多相关文章

  1. Summer sell-off CodeForces - 810B (排序后贪心)

    Summer holidays! Someone is going on trips, someone is visiting grandparents, but someone is trying ...

  2. Codeforces 808 E. Selling Souvenirs(三分)

    E. Selling Souvenirs 题意: n件物品,有重量和价值,重量只有三种1,2,3.问取不超过m重量的物品的价值总和最大是多少.(n<=1e5,w<=3e5) 思路: n*w ...

  3. Codeforces 571B Minimization:dp + 贪心【前后相消】

    题目链接:http://codeforces.com/problemset/problem/571/B 题意: 给你一个长度为n的数列a[i]. 现在你可以随意改变数字的位置,问你 ∑| a[i] - ...

  4. Codeforces Gym101341K:Competitions(DP)

    http://codeforces.com/gym/101341/problem/K 题意:给出n个区间,每个区间有一个l, r, w,代表区间左端点右端点和区间的权值,现在可以选取一些区间,要求选择 ...

  5. HDU 5811 Colosseo(拓扑排序+单调DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5811 [题目大意] 给出 一张单向图,现在将其划分成了两个部分,问划分之后的点是否分别满足按照一定 ...

  6. MySql下实现先排序后分组

    最近在工作中遇到一个先排序后分组的需求,发现MySql不同的版本有不同的结果,特此记录. 举例:要求在shop表中查询出各类型商店中价格最高的商品. --表结构-- create table `sho ...

  7. Codeforces 544E K Balanced Teams (DP)

    题目: You are a coach at your local university. There are nn students under your supervision, the prog ...

  8. Codeforces 1133E - K Balanced Teams - [DP]

    题目链接:https://codeforces.com/contest/1133/problem/C 题意: 给出 $n$ 个数,选取其中若干个数分别组成 $k$ 组,要求每组内最大值与最小值的差值不 ...

  9. <二分查找+双指针+前缀和>解决子数组和排序后的区间和

    <二分查找+双指针+前缀和>解决子数组和排序后的区间和 题目重现: 给你一个数组 nums ,它包含 n 个正整数.你需要计算所有非空连续子数组的和,并将它们按升序排序,得到一个新的包含 ...

随机推荐

  1. C#从http上拿返回JSON数据

    C#如何拿到从http上返回JSON数据? 第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化对象) ...

  2. This network connection does not exist

    This network connection does not exist 在windows server 2008上面map了一个磁盘,共享的folder被我停止共享后,点击该磁盘的disconn ...

  3. 创建 tomcat 服务的镜像

    如何设计 Tomcat 的 Dockerfile $ sudo docker search tomcat |wc -l 285 在 dockerhub 上搜索与 tomcat 相关的镜像,有如此之多的 ...

  4. (转)Spring Boot 2(一):【重磅】Spring Boot 2.0权威发布

    http://www.ityouknow.com/springboot/2018/03/01/spring-boot-2.0.html 就在今天Spring Boot2.0.0.RELEASE正式发布 ...

  5. linux 软连接创建 压缩解压缩 linux的dns服务相关

    linux软连接创建 注意用绝对路径,语法如下 ln -s 目标文件绝对路径 软连接名字绝对路径 ln -s /小护士.txt /tmp/hs.txt 修改linux的PS1变量,命令提示符变量 PS ...

  6. python 之常用模块

    一 认识模块 二 常用模块    (1)re模块    (2)collections模块 一  认识模块     (1)什么是模块      (2)模块的导入和使用 (1)模块是:一个模块就是一个包含 ...

  7. Linux:Day7(上) find、文件特殊权限、if语句

    文件查找: 在文件系统上查找符合条件的文件: 文件查找工具:locate,find locate:非实时查找(数据库查找) find:实时查找: locate: 依赖于事先 构建的索引:索引的构建是在 ...

  8. 3、原生jdbc链接数据库之锁与事务

    一.锁的概念1.作用:是保证数据的一致性,只能一个人修改数据,不能同时多用户修改2.分类:行级锁和表级锁   乐观锁和悲观锁 二.事务1.为了保证数据的一致性和完整性,让数据库的多项操作合并为一个整体 ...

  9. ESP8266烧录配置

    装载的网页在工程目录下同个文件夹data

  10. python 那些我记不清的函数

    eval 函数:用来计算在字符串中的有效Python表达式,并返回一个对象......将字符串变回数据类型 enumerate函数:加上序号 isinstance函数:判断数据类型 isinstanc ...