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

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

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.

思路:见代码

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define MAX 100005 using namespace std;
typedef long long ll; int n,c,d,b,cost,cnt,maxs;
char type; struct node{
int b,cost;
char type;
};
node f[MAX]; bool cmp(node x,node y) {
if(x.b!=y.b)
return x.b>y.b;
else
{
return x.cost<y.cost;
}
}//排序按照美丽值排序,如果相等就按照花费小排 int main() {
while(scanf("%d%d%d",&n,&c,&d)!=EOF) {
maxs=0;
for(int i=0; i<n; i++) {
scanf("%d%d %c",&b,&cost,&type);
f[i].b=b;
f[i].cost=cost;
f[i].type=type;
}//输入
sort(f,f+n,cmp);
int max1=0,max2=0;
for(int i=0; i<n; i++) {
if(f[i].type=='C'&&f[i].cost<=c) {
max1=f[i].b;
break;
} }
for(int i=0; i<n; i++) {
if(f[i].type=='D'&&f[i].cost<=d) {
max2=f[i].b;
break;
}
}//第一种情况,在用硬币的和用钻石的分别一个
int maxxn=0;
//这种情况必须保证都能得到,如果有一个不够就直接是原来的0就行了,表示这种情况不成立
if(max1!=0&&max2!=0) {
maxxn=max1+max2;
}
//第二种情况,都从用硬币的取两个或者都从用钻石的取两个,这样枚举的时候是有技巧的,不然n^2必然超时
//也要庆幸数据没有都卡到最后
for(int i=0; i<n; i++) {
int t1=c;
int t2=d;
if(f[i].type=='C'&&t1<=f[i].cost)
continue;
else if(f[i].type=='D'&&t2<=f[i].cost)
continue;
if(f[i].type=='C'&&t1>f[i].cost) {
t1-=f[i].cost;
cnt=0;
cnt+=f[i].b;
} else if(f[i].type=='D'&&t2>f[i].cost) {
t2-=f[i].cost;
cnt=0;
cnt+=f[i].b;
}
for(int j=i+1; j<n; j++) {
if(f[j].type=='C'&&f[j].cost<=t1) {
cnt+=f[j].b;
maxs=max(maxs,cnt);
break;
} else if(f[j].type=='D'&&f[j].cost<=t2) {
cnt+=f[j].b;
maxs=max(maxs,cnt);
break;
}
}
} printf("%d\n",max(maxs,maxxn)); }
return 0;
}

最新文章

  1. (转) Deep Reinforcement Learning: Playing a Racing Game
  2. 文件上传时jquery.form.js中提示form.submit SCRIPT5: 拒绝访问
  3. linux或者windows下的文件拷贝
  4. openfire+asmack搭建的安卓即时通讯(六) 15.4.16
  5. Ajax的基本语法
  6. const char *p、char const *p、char * const p的区别?
  7. MySQL 全角转换为半角
  8. CSA 第五届研讨会 想象
  9. 数据挖掘实战&lt;1&gt;:数据质量检查
  10. monkey自定义脚本实践
  11. php如何和linux进行通讯
  12. Vue2学习笔记:计算属性(computed)
  13. Key in_hidden/batch_normalization/beta not found in checkpoint
  14. linux_查看磁盘与目录容量
  15. 第 6 章 存储 - 040 - docker managed volume
  16. [Java] 各种流的分类及区别
  17. MQTT压力测试工具之JMeter插件教程
  18. poj2828 线段树单点更新
  19. Spark设计思想浅析
  20. 顺序表[A+B-&gt;C]

热门文章

  1. javascript使用技巧总结,不断更新...
  2. 【摘自大型网站技术架构书】负载均衡时session如何共享
  3. 数据库 MySQL 之 基本概念
  4. 数据库连接池DBUtils
  5. jqgrid 编辑表格(包含下拉框)
  6. PHP中循环结构之foreach循环语句
  7. css总结6:行高和字体大小
  8. 关于Pascal(帕斯卡)以及Camel(驼峰)命名法
  9. Sharepoint2013搜索学习笔记之搜索构架简单概述(一)
  10. Task 回调