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. MapFileParser.sh: Permission denied

    Unity项目,需要用Xcode运行,结果报了错误. 解决方案: 1.打开终端, 2.输入以下命令: chmod +x   /Users/......./MapFileParser.sh (MapFi ...

  2. 网站出现403 Forbidden

    1, 你在一定时间内过多地访问此网站(一般是用采集程序),被防火墙拒绝访问了 2, 网站域名解析到了空间,但空间未绑定此域名 3, 你的网页脚本文件在当前目录下没有执行权限 4, 服务器繁忙,同一IP ...

  3. Shell脚本常用模板

    作为一个运维人员编写Shell脚本是很平常的,一个格式好的脚本不仅赏心悦目,后期自己和别人也易于维护. 下面的脚本就是我自己的shell编写格式,如下: [root@mini05 -]# cat te ...

  4. 更高的压缩比,更好的性能–使用ORC文件格式优化Hive

    http://lxw1234.com/archives/2016/04/630.htm 关键字:orc.index.hive Hive从0.11版本开始提供了ORC的文件格式,ORC文件不仅仅是一种列 ...

  5. nginx基本配置与参数说明

    user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes  1;   #全局错误日志及PID文件 #error_log  logs/error.log; # ...

  6. Alex网络

    alexNet共有八层网络卷积层1:输入224*224*3 卷积核11*11*3*96 步长为4 然后是ReLU.局部归一化.3*3步长为2的最大值池化卷积层2:输入为28*28*96 卷积核5*5* ...

  7. VMware15安装MAC(MAC OS 10.13)(OS X 10.14)原版可升级最新可解锁macOS Unlocker3.0(OS X 10.13)

      目录树 1.1.2安装环境: 1.1.3所需资源: 1.1.4 Unlocker 3.0解锁 1.1.5 配置环境 1.1.6开始安装 1.1.7开启虚拟机进入MAC安装界面 1.1.8 macO ...

  8. 【转】js中通过docment.cookie获取到的内容不完整! 在浏览器的application里的cookie里可以看到完整的cookie,个别字段无法通过document.cookie获取。 是否有其他办法可以获取到??

    js中通过docment.cookie获取到的内容不完整!在浏览器的application里的cookie里可以看到完整的cookie,个别字段无法通过document.cookie获取.是否有其他办 ...

  9. day04 if判断、while条件循环、for迭代器循环部分使用举例

    一:if判断 1.成绩>=90,那么:优秀         如果成绩>=80且<90,那么:良好         如果成绩>=70且<80,那么:普通         其 ...

  10. ORA-4031 During Startup Nomount using RMAN without parameter file (PFILE) (Doc ID 1176443.1)

    ORA-4031 During Startup Nomount using RMAN without parameter file (PFILE) (Doc ID 1176443.1) APPLIES ...