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

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.

#include <cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
/*
感觉就像是背包的变形,只不过我们只是需要两件物品
所以我们只需要保留一件物品的最优值

dp[]中的值都是单一的值,也就是说是一件物品在当前价格下的最大beauty, 
*/
const int maxn=1e5+10;
const int INF=1e9;

struct node{
    int b,p;
    node(int b_,int p_){
       b=b_,p=p_;
    }
    bool operator<(const node other)const{
        return  p < other.p;
    }
};

vector<node> vc,vd;
int dp_coin[maxn],dp_diam[maxn];


int main()
{
    fill(dp_coin,dp_coin+maxn,-INF);
    fill(dp_diam,dp_diam+maxn,-INF);
	int n,sum_coin,sum_diam;
	scanf("%d %d %d",&n,&sum_coin,&sum_diam);
	char type;int b,p;
	for(int i=1;i<=n;i++){
        scanf("%d %d %c",&b,&p,&type);
        if(type=='C'){
             vc.push_back(node(b,p));
        }
        else{
              vd.push_back(node(b,p));
        }
	}
	 sort(vc.begin(),vc.end());
     sort(vd.begin(),vd.end());

	    int max_coin=-INF;
        int size_coin=vc.size();
        int index_coin=0;
        for(int i=0;i<size_coin;i++){
           int val=vc[i].b,cost=vc[i].p;
           for(int j=index_coin+1;j<=cost;j++){
               dp_coin[j]=dp_coin[j-1];//更新点
           }
           int x=min(sum_coin-cost,cost); 
           if(x>=vc[0].p){   //这里的x,就是记录看看能不能买成两件                     
               max_coin=max(max_coin,dp_coin[x]+val);
           }
           dp_coin[cost]=max(dp_coin[cost],val);
           index_coin=cost;
        }
        for(int i=index_coin+1;i<=sum_coin;i++)
            dp_coin[i]=dp_coin[i-1];

        int max_diam=-INF;
        int size_diam=vd.size();
        int index_diam=0;
        for(int i=0;i<size_diam;i++){
            int val=vd[i].b,cost=vd[i].p;
            for(int j=index_diam+1;j<=cost;j++){
                dp_diam[j]=dp_diam[j-1];//更新点
            }
            int x=min(cost,sum_diam-cost);
            if(x>=vd[0].p){
                max_diam=max(max_diam,dp_diam[x]+val);
            }
            dp_diam[cost]=max(dp_diam[cost],val);
            index_diam=cost;
        }
        for(int i=index_diam+1;i<=sum_diam;i++)
            dp_diam[i]=dp_diam[i-1];
            
        int max_mix=dp_coin[sum_coin]+dp_diam[sum_diam];
        int ans=max(max_coin,max_diam);
        ans=max(max_coin,max(max_diam,max(max_mix,0)));
        printf("%d\n",ans);
	    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值