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 n, c 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.
题意: 你有c这么多的硬币, d这么多的钻石;你现在有n个选择,选择中 你要么只能用钻石购买喷泉,要么只能用硬币够买喷泉,问你 你必须购买两个喷泉 所得到的最大美丽值时多少;
思路:学长说这时一个很裸的线段树的题,但是我i并不会线段树,等学会了再来添加方法;
下面来说说用树状数组维护最大值的方法,代码里的注释已经很清楚了就直接看代码吧;
#include<cstdio>
#include<cstring>
#include<iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 5;
int a[maxn]; //用i个硬币购买 得到a[i]的美丽值; 每个位置存放的都是有i这么多的钱能得到的最大的美丽值
int b[maxn]; // 用i个钻石购买 得到b[i]的美丽值;
char ch;
int n, c, d; //n种选择 ,有c这么多的硬币 ,d这么多的钻石
int lowbit(int x) //树状数组的下标改变
{
return x&(-x);
}
void add(int *t, int x, int w){ //将w的美丽值插入到t[x]位置
while(x <= maxn){ //每加入一个值就会影响但大于x以后的最大值
t[x] = max(t[x] ,w); //更新
x += lowbit(x);
}
}
int M(int *t, int x) //查询有x这么多的钱可以得到的最大美丽值
{
int res = 0;
while(x > 0){
res = max(res, t[x]);
x -= lowbit(x);
}
return res;
}
int main()
{
while(~scanf("%d %d %d",&n, &c, &d))
{
int ans = 0;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for(int i = 1;i <= n;i++)
{
int m, q;
char ch;
int maxx = 0;
scanf("%d %d %c", &m ,&q, &ch);
if(ch == 'C')
{
//接下来先默认你要用硬币买这个价钱为q的喷泉
maxx = M(b, d); // 假设另一个喷泉用所有的钻石购买
if(q > c) continue;
maxx = max(maxx, M(a, c - q)); //假设另一个喷泉用剩下的硬币买
add(a, q, m); //在树状数组中加入 因为当前的数据回影响到你接下来的更新
}
else
{
//接下来先默认你要用硬币买这个价钱为q的喷泉
maxx = M(a, c); //假设另一个喷泉用所有的硬币购买
if(q > d) continue;
maxx = max(maxx , M(b, d - q)); //假设另一个喷泉用剩下的钻石买
add(b, q, m);
}
if(maxx > 0) ans = max(ans, maxx + m); //当第二个喷泉能购买时 更新ans
}
printf("%d\n",ans);
}
return 0;
}