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/c++ 线性栈

    c/c++ 线性栈 线性栈 下面的代码实现了以下功能 函数 功能描述 push 压入 pop 弹出 show_list 打印 clear 移动top指针到栈底 destroy 释放所有内存空间 seq ...

  2. Windows Server 2016-WinSer 2016标准版与数据中心版的区别

    今天在整理文章的时候看到有读者问到他现在的测试环境是用的Windows Server 2016标准版,和我现阶段系列文章的环境是否有区别. 其实针对Windows Server 2016 Active ...

  3. 利用Python通过频谱分析和KNN完成iphone拨号的语音识别

    最近这段时间,学校里的事情实在太多了,从七月下旬一直到八月底实验室里基本天天十二点或者通宵,实在是没有精力和时间来写博客.这周老师出国开会,也算有了一个短暂的休息机会,刚好写点有意思的东西. 上周在天 ...

  4. 简述KVM架构和Xen架构

    暑假最后一篇更新,因为,,,明天我就回学校了. 以下均为个人理解,如果有不对的地方还望各位dalao不吝赐教. 虚拟化 虚拟化是通过Hypervisor程序实现的,Hypervisor的作用是将硬件虚 ...

  5. 力扣算法题—051N皇后问题

    #include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...

  6. MapReduce过程详解及其性能优化

    http://blog.csdn.net/aijiudu/article/details/72353510 废话不说直接来一张图如下: 从JVM的角度看Map和Reduce Map阶段包括: 第一读数 ...

  7. 安装站点时出现“连接数据库出现数据库server或登录password无效,无法连接数据库,请又一次设定”解决方法

    在安装站点时出现  "连接数据库出现数据库server或登录password无效,无法连接数据库,请又一次设定"  可是数据库username和password是正确的,在serv ...

  8. Java-Method类常用方法详解

    一.Method类的定义Method类位于 java.lang.reflect 包中,主要用于在程序运行状态中,动态地获取方法信息二.Method类的常用方法  1.getAnnotatedRetur ...

  9. 猜数字游戏,判断输入的数字与系统产生的数字是否一致(Math.random()与if嵌套循环)

    package com.summer.cn; import java.util.Scanner; public class Test041509 { /** * java 随机数 Math * Mat ...

  10. Feature Extractor[DenseNet]

    0.背景 随着CNN变得越来越深,人们发现会有梯度消失的现象.这个问题主要是单路径的信息和梯度的传播,其中的激活函数都是非线性的,从而特别是乘法就可以使得随着层数越深,假设将传统的神经网络的每一层看成 ...