codeforces 799C Fountains

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 0000 ≤ 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.

Example
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各买一件,另外两种情况有点麻烦。

可以用线段树来做,维护一个区间最大值,假设现在钱数为k,就查询1-k的最大值。

选段树刚建完之后每个点的值都是零,枚举每一个喷泉的时候先查询区间再进行点更新,这样会避免重复购买


#include <cstdio>
#include <algorithm>
using namespace std;
const int M = 1e5 + 5;
int a[M], b[M], acost[M], bcost[M];
struct Seg
{
	int l, r, val;
}seg[M<<2];
void pushUp(int rt)
{
	seg[rt].val = max(seg[rt<<1].val, seg[rt<<1|1].val);
}
void build(int rt, int L, int R)
{
	seg[rt].l = L;
	seg[rt].r = R;
	seg[rt].val = 0;
	if(L==R)
		return;
	int m = (L + R) >> 1;
	build(rt<<1, L, m);
	build(rt<<1|1, m+1, R);
	pushUp(rt);
}
void update(int rt, int ind, int ch)
{
	if(seg[rt].l==seg[rt].r)
	{
		seg[rt].val = max(ch, seg[rt].val);
		return;
	}
	int m = (seg[rt].l + seg[rt].r) >> 1;
	if(ind <= m)
		update(rt<<1, ind, ch);
	else
		update(rt<<1|1, ind, ch);
	pushUp(rt);
}
int query(int rt, int L, int R)
{
	if(seg[rt].l>=L && seg[rt].r<=R)
		return seg[rt].val;
	int m = (seg[rt].l + seg[rt].r) >> 1;
	int t1 = 0, t2 = 0;
	if(L <= m)
		t1 = query(rt<<1, L, R);
	if(R > m)
		t2 = query(rt<<1|1, L, R);
	return max(t1, t2);
}
int main()
{
	int n, C, D, maxc, ans, t1, t2, bt, pt;
	int acnt = 0, bcnt = 0;
	char typ;
	maxc = ans = 0;
	scanf("%d%d%d", &n, &C, &D);
	for(int i=0;i<n;i++)
	{
		scanf("%d%d %c", &bt, &pt, &typ);
		if(typ=='C')
		{
			a[acnt] = bt;
			acost[acnt++] = pt;
		}
		else
		{
			b[bcnt] = bt;
			bcost[bcnt++] = pt;
		}
		maxc = max(maxc, pt);
	}
	maxc = max(maxc, max(C, D));
	t1 = t2 = 0;
	for(int i=0;i<acnt;i++)
	{
		if(C>=acost[i])
			t1 = max(t1, a[i]);
	}
	for(int i=0;i<bcnt;i++)
	{
		if(D>=bcost[i])
			t2 = max(t2, b[i]);
	}
	if(t1!=0&&t2!=0)
		ans = t1 + t2;
	build(1, 1, maxc);
	for(int i=0;i<acnt;i++)
	{
		t1 = 0;
		if(C>acost[i])
			t1 = query(1, 1, C-acost[i]);
		update(1, acost[i], a[i]);
		if(t1!=0)
			ans = max(ans, a[i]+t1);
	}
	build(1, 1, maxc);
	for(int i=0;i<bcnt;i++)
	{
		t1 = 0;
		if(D>bcost[i])
			t1 = query(1, 1, D-bcost[i]);
		update(1, bcost[i], b[i]);
		if(t1!=0)
			ans = max(ans, b[i]+t1);
	}
	printf("%d", ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值