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.
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.
Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.
3 7 6 10 8 C 4 3 C 5 6 D
9
2 4 5 2 5 C 2 1 D
0
3 10 10 5 5 C 5 5 C 10 11 D
10
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 和 D。每一喷泉的代价值是两种之中的一种。一开始有一定数量的 C 和一定数量的 D,要求利用拥有的 C 和 D 建造两个喷泉,使它们的总美丽值最大。如果不足以建成两个喷泉,总美丽值记为0.
比赛的时候用暴搜过了 pretest ,最终还是被 final test 卡掉了。
后来改进的思路是 枚举第一个喷泉,然后用线段树维护区间最大值,用来找它的最佳组合。
代码太长,不看也罢。记得这个线段树找最大值的思路就好。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define fin freopen("in.txt", "r", stdin)
#define mem(a,b) memset(a, b, sizeof(a));
#define rep(a,b,c) for(int a= b; a< c; a++)
#define per(a,b,c) for(int a= c; a> b; a--)
#define x(i) tab[i].x
#define y(i) tab[i].y
#define z(i) tab[i].z
using namespace std;
typedef long long ll;
struct P
{
int x, y, z;
};
const int maxn = 100000 + 10;
int a[2];
P tab[maxn];
int in[2][maxn];
int Seg[2][maxn * 4];
int v[2];
int n;
int ans = 0;
bool chong[maxn];
void build (int p, int l, int r, int z)
{
if(l == r){
Seg[z][p] = in[z][l];
return;
}
int md = (l + r) >> 1;
build(2*p, l, md, z);
build(2*p+1, md+1, r, z);
Seg[z][p] = max(Seg[z][2*p], Seg[z][2*p+1]);
}
void add (int p, int l, int r, int z, int x, int y)
{
if(l == r){
Seg[z][p] += y;
return;
}
int md = (l + r) >> 1;
if(x <= md) add(2*p, l, md, z, x, y);
if(x > md) add(2*p+1, md+1, r, z, x, y);
Seg[z][p] = max(Seg[z][2*p], Seg[z][2*p+1]);
}
int query (int p, int l, int r, int z, int x, int y)
{
if(x <= l && y >= r) return Seg[z][p];
int md = (l + r) >> 1;
int Max = 0;
if(x <= md) Max = max(Max, query(2*p, l, md, z, x, y));
if(y > md) Max = max(Max, query(2*p+1, md+1, r, z, x, y));
return Max;
}
int main ()
{
while(cin >> n >> v[0] >> v[1]){
for(int i= 0; i< n; i++){
char c;
scanf("%d %d %c", &tab[i].x, &tab[i].y, &c);
if(c == 'C') tab[i].z = 0;
else tab[i].z = 1;
if(in[z(i)][y(i)] == x(i)) chong[i] = true;
if(in[z(i)][y(i)] < x(i)) in[z(i)][y(i)] = x(i);
}
build(1, 1, maxn, 0);
build(1, 1, maxn, 1);
for(int i= 0; i< n; i++){
if(y(i) > v[z(i)]) continue;
int B = x(i);
if(!chong[i]) add(1, 1, maxn, z(i), y(i), -x(i));
int a = v[z(i)]-y(i) > 0 ? query(1, 1, maxn, z(i), 1, v[z(i)]-y(i)) : 0;
int b = query(1, 1, maxn, !z(i), 1, v[!z(i)]);
B += max(a, b);
if(!chong[i]) add(1, 1, maxn, z(i), y(i), x(i));
if(B > x(i) && B > ans) ans = B;
}
cout << ans << endl;
}
return 0;
}