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.

思路:见代码

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define MAX 100005

using namespace std;
typedef long long ll;

int n,c,d,b,cost,cnt,maxs;
char type;

struct node{
	int b,cost;
	char type;
};
node f[MAX];

bool cmp(node x,node y) {
	if(x.b!=y.b)
	return x.b>y.b;
	else
	{
	 return x.cost<y.cost;
	}
}//排序按照美丽值排序,如果相等就按照花费小排 

int main() {
	while(scanf("%d%d%d",&n,&c,&d)!=EOF) {
		maxs=0;
		for(int i=0; i<n; i++) {
			scanf("%d%d %c",&b,&cost,&type);
			f[i].b=b;
			f[i].cost=cost;
			f[i].type=type;
		}//输入 
		sort(f,f+n,cmp);
		int max1=0,max2=0;
		for(int i=0; i<n; i++) {
			if(f[i].type=='C'&&f[i].cost<=c) {
				max1=f[i].b;
				break;
			}

		}
		for(int i=0; i<n; i++) {
			if(f[i].type=='D'&&f[i].cost<=d) {
				max2=f[i].b;
				break;
			}
		}//第一种情况,在用硬币的和用钻石的分别一个 
		int maxxn=0;
		//这种情况必须保证都能得到,如果有一个不够就直接是原来的0就行了,表示这种情况不成立 
		if(max1!=0&&max2!=0) {
			maxxn=max1+max2;
		}
        //第二种情况,都从用硬币的取两个或者都从用钻石的取两个,这样枚举的时候是有技巧的,不然n^2必然超时
		//也要庆幸数据没有都卡到最后  
		for(int i=0; i<n; i++) {
			int t1=c;
			int t2=d;
			if(f[i].type=='C'&&t1<=f[i].cost)
				continue;
			else if(f[i].type=='D'&&t2<=f[i].cost)
				continue;
			if(f[i].type=='C'&&t1>f[i].cost) {
				t1-=f[i].cost;
				cnt=0;
				cnt+=f[i].b;
			} else if(f[i].type=='D'&&t2>f[i].cost) {
				t2-=f[i].cost;
				cnt=0;
				cnt+=f[i].b;
			}
			for(int j=i+1; j<n; j++) {
				if(f[j].type=='C'&&f[j].cost<=t1) {
					cnt+=f[j].b;
					maxs=max(maxs,cnt);
					break;
				} else if(f[j].type=='D'&&f[j].cost<=t2) {
					cnt+=f[j].b;
					maxs=max(maxs,cnt);
					break;
				}
			}
		}
 

		printf("%d
",max(maxs,maxxn));

	}
	return 0;
}
原文地址:https://www.cnblogs.com/Staceyacm/p/10781796.html