【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 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,然后和两种货币的拥有数目,之后输入n行数据,解释每个井的情况,咱们要在已有资产的前提下,求出两个井的最大美值。

解题思路:刚上来是想直接按照魅力值降序排列一下,之后直接找满足条件的两个井,但是会wa 因为这样做只是考虑的一种比较简单的情况假设数据为 3 10 5   , 9 9 C,5 5 C, 5 6 C。这样输出9而不是正确的11 。 所以,为了解决这个问题 ,决定开树状数组来求解。实时更新树状数组的值,以花费为下标,魅力值为数值。

ac代码:

#include
#include
using namespace std;
#define maxn 100010
int n,c,d;
struct node{
	int bea,p;
};
struct node cc[maxn],dd[maxn];
int tt[maxn];
int lowbit(int x)
{
	return x&-x;
}
int query(int x)
{
	int ans=0;
	while(x>0)
	{
		ans=max(ans,tt[x]);
		x-=lowbit(x);
	}
	return ans;
}
void updata(int x,int y,int val)
{
	while(x>b>>p>>s;
			if(s=='C')
			{
				if(p<=c)
				{
				cc[cnt1].bea=b;
				cc[cnt1++].p=p;
				ans1=max(ans1,b);
				}
			}
			else
			{
				if(p<=d)
				{
				dd[cnt2].bea=b;
				dd[cnt2++].p=p;
				ans2=max(ans2,b);
				}
			}
		}
		int ans=0;
		if(ans1!=-1&&ans2!=-1)
			ans=ans1+ans2;
		memset(tt,0,sizeof(tt));
		for(int i=0;i0)
			{
				ans=max(ans,cur+cc[i].bea);
			}
			updata(cc[i].p,maxn,cc[i].bea);
		}
		memset(tt,0,sizeof(tt)); 
		for(int i=0;i0)
			{
				ans=max(ans,cur+dd[i].bea);
			}
			updata(dd[i].p,maxn,dd[i].bea);
		}
		printf("%d\n",ans);
	}
	return 0;
}

 

你可能感兴趣的:(知识储备,acm训练)