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;
}

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. 190. Reverse Bits 二进制相反数

    [抄题]: Reverse bits of a given 32 bits unsigned integer. Example: Input: 43261596 Output: 964176192 E ...

  2. Python PyPI中国镜像

    from:http://blog.makto.me/post/2012-11-01/pypi-mirror from:http://www.pypi-mirrors.org/ from:http:// ...

  3. Django ——Timezone 处理

    Django ——Timezone 处理 https://blog.csdn.net/qq_37049781/article/details/79347278 2018年02月22日 14:50:24 ...

  4. 第十三课 Actionlib(2)

    上节课讲到了客户端,这节课讲解一下服务器 1.创建服务器源文件touch fibonacciserver.cpp 2.编写源文件 3.修改CMakeLists.txt 4.编译之catkin_make ...

  5. ASP.NET MVC5 Authentication Filters执行链

    注意区分认证和授权: The following  are the differences in short: Authentication(认证): It is a process of verif ...

  6. poj2299 Ultra-QuickSort(线段树求逆序对)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  7. C#验证身份证号码正确性

    18位号码: private static bool CheckIDCard18(string Id) { ; ), , ) || '), out n) == false) { return fals ...

  8. PLSQL Developer连接远程Oracle

    注:内容来网络 (一)不安装客户端的解决办法. 第一种方法: 1.在安装ORACLE服务器的机器上搜索下列文件, oci.dll ocijdbc10.dll ociw32.dll orannzsbb1 ...

  9. ubuntu14.04,安装Gnome 15.10 (桌面)

    Linux:ubuntu14.04 Gnome:15.10 更新最新版Gnome的一个好处:更新了ubuntu的软件源,我们可以使用ubuntu的软件中心获取更多需要的软件!! ubuntu默认的桌面 ...

  10. 前端中onload与ready的区别

    Jquery的ready()与Javascrpit的load() 1 window.onload() $(document).ready() 加载时机 必须等待网页全部加载完毕(包括图片等),然后再执 ...