Fountains CodeForces - 799C 贪心+排序

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 个温泉,有 C 个金币, D 枚钻石。现在要建两个温泉,每个温泉有一个价值,还有价格,但价格只是一种类型,即是用金币或钻石,也就是说每个温泉只能单独用金币或钻石。要在已有金币和钻石内,建两个温泉,求最后的最大价值。

思路:首先按照价值从大到小排序,如果价值相同的就按照价格从小到大排序。当我们选中一个喷泉编号为i时,我们从i+1开始,找一个在当前剩余金币或钻石允许范围内的喷泉,维护价值最大值。最终得到的就是答案。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=100009;
struct node{
	int val;
	int cost;
	char s[3];
}a[maxn];
bool cmp(node x,node y)
{
	if(x.val==y.val)
		return x.cost<y.cost;
	else
		return x.val>y.val;
}
int main()
{
	int n,c,d;
	scanf("%d%d%d",&n,&c,&d);
	for(int i=1;i<=n;i++)
		scanf("%d%d%s",&a[i].val,&a[i].cost,a[i].s);
	sort(a+1,a+1+n,cmp);
	int flag,sum,maxx=0;
	for(int i=1;i<=n;i++)
	{
		flag=0;sum=0;
		int cc=c,dd=d;
		if(a[i].s[0]=='C')
		{
			if(a[i].cost<=cc)
			{
				cc-=a[i].cost;
				sum+=a[i].val;
				flag=1;
			}
		}
		else
		{
			if(a[i].cost<=dd)
			{
				dd-=a[i].cost;
				sum+=a[i].val;
				flag=1;
			}
		}
		if(flag)
		{
			for(int j=i+1;j<=n;j++)
			{
				if(a[j].s[0]=='C'&&a[j].cost<=cc)
				{
					sum+=a[j].val;
					maxx=max(maxx,sum);
					break;
				}
				if(a[j].s[0]=='D'&&a[j].cost<=dd)
				{
					sum+=a[j].val;
					maxx=max(maxx,sum);
					break;
				}
			}
		}
	}
	printf("%d\n",maxx);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/why932346109/article/details/88803110
今日推荐