Fountains

CodeForces - 799C

某土豪想要造两座喷泉。现在有 n 个造喷泉的方案,我们已知每个方案的价格以及美观度。有两种合法的货币:金币和钻石。这两种货币之间不能以任何方式转换。

找出一种合法方案使得两座喷泉的美观度和最大。

Input

第一行包含 3 个整数 n, cd (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 000) — 表示造喷泉的方案数,金币的数量和钻石的数量。

之后 n 行描述每一种建造喷泉的方案。每行包含两个整数 bipi (1 ≤ bi, pi ≤ 100 000) — 分别表示第 i 个喷泉的美观度和价格,之后一个字符"C" 或 "D", 表示这个价格使用金币还是钻石描述的。

Output

输出两个美观度最大的喷泉的美观度和。如果没有合法方案,输出 0.

Example

Input
3 7 6
10 8 C
4 3 C
5 6 D
Output
9
Input
2 4 5
2 5 C
2 1 D
Output
0
Input
3 10 10
5 5 C
5 5 C
10 11 D
Output
10

Note

第一个样例中造第2个喷泉,美观度为 4, 价格为 3 金币。第一个喷泉没有足够的金币来造。同时还造第3个喷泉,美观度为 5 ,价格为 6 钻石。那么总美观度为 9。

第二个样例中没有合法方案。

可以预处理出

pre1[N][2]

表示花费N个金币,能够获得的最大美丽值和次小美丽值;

然后对以下3种情况枚举其中的一个;

一.

两个商品都是金币买的

则枚举其中一个商品是哪一个;

然后看看余额还剩多少->rest;

直接获取pre1[rest][0]

如果枚举的商品的价格<=rest且pre1[rest][0]和这个枚举的商品的美丽值一样

就取pre1[rest][1];即次小值;

二.

两个商品都是钻石买的,同上

三.

一个商品是钻石买的,另外一个是金币买的,各自单独找最大值;

#include <bits/stdc++.h>
#define rep1(i,x,y) for(int i=x;i<=y;i++)
#define rep2(i,x,y) for(int i=y;i>=x;i--)
#define pb push_back
#define long long ll
using namespace std;
const int N=2e5+100; struct node
{
int b;//beauty
int p;//price
int id;
};
vector<int> g[2][N];//the beauty of two types fountain from each price
int bo[2][N][2];
int n,c,d;
node a[N];
int main()
{
ios_base::sync_with_stdio(0);
cin>>n>>c>>d;
char s[3];
rep1(i,1,n)
{
cin>>a[i].b>>a[i].p;
cin>>s;
a[i].id=(s[0]=='C') ? 0:1;
g[a[i].id][a[i].p].pb(a[i].b);
}
rep1(k,0,1)
{
rep1(i,1,100000)
{
bo[k][i][1]=bo[k][i-1][1];//DP
bo[k][i][0]=bo[k][i-1][0];//DP
int len=g[k][i].size();
rep1(j,0,len-1)
{
if(g[k][i][j]>bo[k][i][0])
{//g[k(*id)][i(*price)][j(*beauty)]
bo[k][i][1]=bo[k][i][0];
bo[k][i][0]=g[k][i][j];
//bo[k][i][0] find the best beauty and
//bo[k][i][0] the second beauty
//of fountains in the same price by coins
//(or diamonds,depends on "k")
}
else if(g[k][i][j]>bo[k][i][1])
bo[k][i][1]=g[k][i][j];
}
}
}
//all coins
int ans=0;
rep1(i,1,n)
if (a[i].id==0)
{
int t1 = a[i].b,t2 = a[i].p;
int rest = c-t2;
if (rest<=0) continue;
int t3 = bo[0][rest][0];
if (t2<=rest && t1==t3)
t3 = bo[0][rest][1];
if (t3<=0) continue;
ans = max(ans,t1+t3);
}
//all diamonds
rep1(i,1,n)
if (a[i].id==1)
{
int t1 = a[i].b,t2 = a[i].p;
int rest = d-t2;
if (rest<=0) continue;
int t3 = bo[1][rest][0];
if (t2<=rest && t1==t3)
t3 = bo[1][rest][1];
if (t3<=0) continue;
ans = max(ans,t1+t3);
}
//half coin half diamond
rep1(i,1,n)
{
int t1 = a[i].b,t2 = a[i].p;
int rest,t3;
if (a[i].id==0)
{
rest = d;
if (t2>c) continue;
}
else
{
rest = c;
if (t2>d) continue;
}
if (rest<=0) continue;
t3 = bo[1-a[i].id][rest][0];
if (t3<=0) continue;
ans = max(ans,t1+t3);
}
cout<<ans<<endl;
return 0;
}

最新文章

  1. 【代码笔记】iOS-获得当前硬盘空间
  2. vim
  3. 【译】Permissions Best Practices Android M权限最佳做法
  4. salesforce 零基础开发入门学习(八)数据分页简单制作
  5. SpringMyBatis解析3-MapperFactoryBean
  6. CSS3中媒体查询,更换样式表
  7. myeclipse-建立webservice服务端和客户端
  8. matlab 直方图均衡化
  9. jQuery.each(object, [callback])方法,用于处理json数组
  10. Lazarus解决含中文文件名或路径的使用问题
  11. [转] Android root 原理
  12. 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】
  13. activeMq 使用方法
  14. IE6的兼容性以及处理方法
  15. Unity3D学习笔记(五)C#与JavaScript组件访问的比较
  16. 用Laravel Sms实现 laravel短信验证码的发送
  17. loj2977 巧克力 (斯坦纳树+随机化)
  18. Git&amp;GitHub语法大全
  19. 第16月第15天 glut
  20. IAR EWAR 内联汇编 调用外部函数 Error[Og005], Error[Og006]

热门文章

  1. 【笔记】ROC曲线
  2. 通过Mysql提权的几种姿势
  3. VLAN-3 Hybrid接口应用
  4. 012 PCIe总线的基础知识
  5. CF1264D2 Beautiful Bracket Sequence
  6. 在ASP.NET Core调用WebService
  7. 如何修改leaflet的marker图标
  8. LeetCoded第242题题解--java--数组
  9. spring初始化源码浅析之关键类和扩展接口
  10. 多线程编程&lt;一&gt;