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.
题意:给你n个喷泉,两种类型的金币数量,C类金币只能修建C类喷泉,D类金币只能修建D类喷泉
每个喷泉带来的魅力值是不一样的,问让你修且仅修2个喷泉可以获得的最大魅力值
思路 :分有三种情况,第一种是,C类喷泉,D类喷泉各修一个,第二种是用C币修两个C类喷泉;第三种用D币修两个D类喷泉;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#define pb push_back
#define pp pair<int,int>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
vector<pp> a,b;
int p[maxn],h[maxn];
int main()
{
int n,C,D;
scanf("%d%d%d",&n,&C,&D);
for(int i=0;i<n;i++)
{
int x,y;
char op[2];
scanf("%d%d%s",&x,&y,op);
if(op[0]=='C')
a.pb(pp(y,x));
else
b.pb(pp(y,x));
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
int ans=0,res=0,flag=0;
for(int i=0;i<a.size();i++)
{
if(i==0)
p[i]=a[i].second;
else
p[i]=max(p[i-1],a[i].second);
if(C>=a[i].first)
ans=max(ans,a[i].second);
}
for(int i=0;i<b.size();i++)
{
if(i==0)
h[i]=b[i].second;
else
h[i]=max(h[i-1],b[i].second);
if(D>=b[i].first)
res=max(res,b[i].second);
}
if(ans&&res)//CD;
ans+=res,flag=1;
for(int i=1;i<a.size();i++)//CC
{
if(a[i].first>C)break;
int aim=C-a[i].first;
int temp=a[i].second;
int l=0,r=i-1,pos=-1;
int mid;
while(l<=r)
{
mid=r+l>>1;
if(aim>=a[mid].first)
l=mid+1,pos=mid;
else
r=mid-1;
}
if(pos!=-1&&p[pos])
ans=max(ans,temp+p[pos]),flag=1;
}
for(int i=1;i<b.size();i++)//DD
{
if(b[i].first>D)break;
int aim=D-b[i].first;
int temp=b[i].second;
int l=0,r=i-1,pos=-1;
int mid;
while(l<=r)
{
mid=r+l>>1;
if(aim>=b[mid].first)
l=mid+1,pos=mid;
else
r=mid-1;
}
if(pos!=-1&&h[pos])
ans=max(ans,temp+h[pos]),flag=1;
}
if(flag==0)
ans=0;
printf("%d\n",ans);
return 0;
}