Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are n available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cost can be either in coins or diamonds. No money changes between the types are allowed.

Help Arkady to find two fountains with maximum total beauty so that he can buy both at the same time.

Input

The first line contains three integers nc and d (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 000) — the number of fountains, the number of coins and diamonds Arkady has.

The next n lines describe fountains. Each of these lines contain two integers bi and pi (1 ≤ bi, pi ≤ 100 000) — the beauty and the cost of the i-th fountain, and then a letter "C" or "D", describing in which type of money is the cost of fountain i: in coins or in diamonds, respectively.

Output

Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.

Examples

Input

  1. 3 7 6
  2. 10 8 C
  3. 4 3 C
  4. 5 6 D

Output

  1. 9

Input

  1. 2 4 5
  2. 2 5 C
  3. 2 1 D

Output

  1. 0

Input

  1. 3 10 10
  2. 5 5 C
  3. 5 5 C
  4. 10 11 D

Output

  1. 10

Note

In the first example Arkady should build the second fountain with beauty 4, which costs 3 coins. The first fountain he can't build because he don't have enough coins. Also Arkady should build the third fountain with beauty 5 which costs 6 diamonds. Thus the total beauty of built fountains is 9.

In the second example there are two fountains, but Arkady can't build both of them, because he needs 5 coins for the first fountain, and Arkady has only 4 coins.

思路:见代码

代码:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<queue>
  6. #include<stack>
  7. #include<map>
  8. #include<set>
  9. #include<cmath>
  10. #define MAX 100005
  11. using namespace std;
  12. typedef long long ll;
  13. int n,c,d,b,cost,cnt,maxs;
  14. char type;
  15. struct node{
  16. int b,cost;
  17. char type;
  18. };
  19. node f[MAX];
  20. bool cmp(node x,node y) {
  21. if(x.b!=y.b)
  22. return x.b>y.b;
  23. else
  24. {
  25. return x.cost<y.cost;
  26. }
  27. }//排序按照美丽值排序,如果相等就按照花费小排
  28. int main() {
  29. while(scanf("%d%d%d",&n,&c,&d)!=EOF) {
  30. maxs=0;
  31. for(int i=0; i<n; i++) {
  32. scanf("%d%d %c",&b,&cost,&type);
  33. f[i].b=b;
  34. f[i].cost=cost;
  35. f[i].type=type;
  36. }//输入
  37. sort(f,f+n,cmp);
  38. int max1=0,max2=0;
  39. for(int i=0; i<n; i++) {
  40. if(f[i].type=='C'&&f[i].cost<=c) {
  41. max1=f[i].b;
  42. break;
  43. }
  44. }
  45. for(int i=0; i<n; i++) {
  46. if(f[i].type=='D'&&f[i].cost<=d) {
  47. max2=f[i].b;
  48. break;
  49. }
  50. }//第一种情况,在用硬币的和用钻石的分别一个
  51. int maxxn=0;
  52. //这种情况必须保证都能得到,如果有一个不够就直接是原来的0就行了,表示这种情况不成立
  53. if(max1!=0&&max2!=0) {
  54. maxxn=max1+max2;
  55. }
  56. //第二种情况,都从用硬币的取两个或者都从用钻石的取两个,这样枚举的时候是有技巧的,不然n^2必然超时
  57. //也要庆幸数据没有都卡到最后
  58. for(int i=0; i<n; i++) {
  59. int t1=c;
  60. int t2=d;
  61. if(f[i].type=='C'&&t1<=f[i].cost)
  62. continue;
  63. else if(f[i].type=='D'&&t2<=f[i].cost)
  64. continue;
  65. if(f[i].type=='C'&&t1>f[i].cost) {
  66. t1-=f[i].cost;
  67. cnt=0;
  68. cnt+=f[i].b;
  69. } else if(f[i].type=='D'&&t2>f[i].cost) {
  70. t2-=f[i].cost;
  71. cnt=0;
  72. cnt+=f[i].b;
  73. }
  74. for(int j=i+1; j<n; j++) {
  75. if(f[j].type=='C'&&f[j].cost<=t1) {
  76. cnt+=f[j].b;
  77. maxs=max(maxs,cnt);
  78. break;
  79. } else if(f[j].type=='D'&&f[j].cost<=t2) {
  80. cnt+=f[j].b;
  81. maxs=max(maxs,cnt);
  82. break;
  83. }
  84. }
  85. }
  86. printf("%d\n",max(maxs,maxxn));
  87. }
  88. return 0;
  89. }

Fountains(非线段树版(主要是不太会用))的更多相关文章

  1. Matrix(线段树版)

    poj2155:http://poj.org/problem?id=2155 题意;同上一遍随笔. 题解:这里用二维线段树打了一发.第一次学习别人的代码.才学的.这种树套树的程序,确实很费脑子,一不小 ...

  2. BZOJ2028:[SHOI2009]会场预约(线段树版)

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  3. Color the ball HDU - 1556 (非线段树做法)

    题意:在1到n的气球中,在不同的区域中涂颜色,问每个气球涂几次. #include<cstdio>int num[100010];int main(){ int n, x, y;; whi ...

  4. hdu 1556 Color the ball(非线段树做法)

    #include<stdio.h> #include<string.h> ]; int main() { int n,i; int a,b; while(scanf(" ...

  5. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  6. zkw线段树详解

    转载自:http://blog.csdn.net/qq_18455665/article/details/50989113 前言 首先说说出处: 清华大学 张昆玮(zkw) - ppt <统计的 ...

  7. Luogu P3740 [HAOI2014]贴海报_线段树

    线段树版的海报 实际上这个与普通的线段树相差不大,只是貌似数据太水,暴力都可以过啊 本来以为要离散的,结果没打就A了 #include<iostream> #include<cstd ...

  8. 洛谷.3733.[HAOI2017]八纵八横(线性基 线段树分治 bitset)

    LOJ 洛谷 最基本的思路同BZOJ2115 Xor,将图中所有环的异或和插入线性基,求一下线性基中数的异或最大值. 用bitset优化一下,暴力的复杂度是\(O(\frac{qmL^2}{w})\) ...

  9. Gym 101911E "Painting the Fence"(线段树区间更新+双端队列)

    传送门 题意: 庭院中有 n 个围栏,每个围栏上都被涂上了不同的颜色(数字表示): 有 m 条指令,每条指令给出一个整数 x ,你要做的就是将区间[ x第一次出现的位置 , x最后出现的位置 ]中的围 ...

随机推荐

  1. cocos2d-x 在vs2010下的搭建(win7系统)

    1从官网下载cocos2d-x2.1.3的源码地址如下: http://cocos2d-x.org/ 2.解压下载的软件包我们会发现红框中vs2010的项目文件双击打开它 3.打开后我们要生成一些wi ...

  2. SpringBoot27 JDK动态代理详解、获取指定的类类型、动态注册Bean、接口调用框架

    1 JDK动态代理详解 静态代理.JDK动态代理.Cglib动态代理的简单实现方式和区别请参见我的另外一篇博文. 1.1 JDK代理的基本步骤 >通过实现InvocationHandler接口来 ...

  3. DBMS ODBC JDBC是什么?

    昨天躺在被窝里面看了几页电子书,今天写下来. 数据库就是存放数据的仓库. DBMS的意思是数据库管理系统,作用就是“管理”数据库的.“管理”这两个字简单说来就是“增删改查”.所以DBMS就是能够对数据 ...

  4. Django框架 之 中间件

    Django框架 之 中间件 浏览目录 中间件介绍 自定义中间件 中间件的执行流程 中间件版登录验证 一.中间件介绍 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个 ...

  5. 对request,session,application作用域形象理解

    看到一篇比较有意思的文章,分享一下.原网址:http://blog.csdn.net/rushkid02/article/details/8063792 几乎所有的Web开发语言都支持Session功 ...

  6. python 测试报告发送邮件

    使用过程成出现的如下错误 smtplib.SMTPDataError: (554, 'DT:SPM 126 smtp5错误解决办法   1.自动化测试中,调用邮件模块自动发送邮件时,运行脚本报错: s ...

  7. 20145218张晓涵_Exp5 MSF基础应用

    20145218张晓涵_Exp5 MSF基础应用 实验原理 MS08-067漏洞描述 MS08-067漏洞的全称为"Windows Server服务RPC请求缓冲区溢出漏洞",如果 ...

  8. Java集合类总结 (二)

    LinkedList类 由于基于数组的链表有一个大的缺点,那就是从链表中间移除一个元素时需要将此元素后面的所有元素向前移动,会产生大量的开销,同样的在链表中间插入一个新元素也会有大量开销.如下图: L ...

  9. SQL 2005报错之Restore fail for Server 'DatabaseServerName'.

    Restore fail for Server 'DatabaseServerName'.(Microsoft.SqlServer.Smo) Additional information: Syste ...

  10. I-team 博客全文检索 Elasticsearch 实战

    一直觉得博客缺点东西,最近还是发现了,当博客慢慢多起来的时候想要找一篇之前写的博客很是麻烦,于是作为后端开发的楼主觉得自己动手丰衣足食,也就有了这次博客全文检索功能Elasticsearch实战,这里 ...