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.
#include <iostream>
#include <cstdio>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <cmath>
using namespace std;
#define LL long long
const int MAX = 1e5 + 50;
int n;
int a[MAX], tree1[MAX], tree2[MAX];
int lowbit(int x){
return x & (-x);
}
void updata1(int i, int k){
while(i <= MAX){
tree1[i] = max(tree1[i], k);
i += lowbit(i);
}
}
int getmax1(int i){
int res = 0;
while(i > 0){
res = max(res, tree1[i]);
i -= lowbit(i);
}
return res;
}
void updata2(int i, int k){
while(i <= MAX){
tree2[i] = max(k, tree2[i]);
i += lowbit(i);
}
}
int getmax2(int i){
int res = 0;
while(i > 0){
res = max(res, tree2[i]);
i -= lowbit(i);
}
return res;
}
int main()
{
int c, d;
scanf("%d%d%d", &n, &c, &d);
int ans = 0;
for(int i = 0; i < n; i++){
int b, p;
char ch;
scanf("%d%d %c", &b, &p, &ch);
if(ch == 'C' && p <= c){
if(getmax2(d)){ // 因为必须买两个,不能少,所以若买不到第二个,则不更新
flag = 1;
ans = max(ans, b + getmax2(d));
}
if(c - p > 0){
if(getmax1(c - p)){
flag = 1;
ans = max(ans, b + getmax1(c - p));
}
}
updata1(p, b);
}
if(ch == 'D' && p <= d){
if(getmax1(c)){
flag = 1;
ans = max(ans, b + getmax1(c));
}
if(d - p > 0){
if(getmax2(d - p)){
flag = 1;
ans = max(ans, b + getmax2(d - p));
}
}
updata2(p, b);
}
}
printf("%d\n", ans);
return 0;
}