(树状数组)Codeforces Round #413 C-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.

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.

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <queue>
 8 #include <set>
 9 #include <map>
10 #include <list>
11 #include <stack>
12 #define mp make_pair
13 typedef long long ll;
14 typedef unsigned long long ull;
15 const int MAX=1e5;
16 const int INF=1e9+5;
17 const double M=4e18;
18 using namespace std;
19 const int MOD=1e9+7;
20 typedef pair<int,int> pii;
21 const double eps=0.000000001;
22 int C[MAX+5],D[MAX+5];
23 void add(int *tree,int k,int num)
24 {
25     while(k<=MAX)
26     {
27         tree[k]=max(tree[k],num);
28         k+=k&(-k);
29     }
30 }
31 int read(int *tree,int k)
32 {
33     int re=0;
34     while(k)
35     {
36         re=max(re,tree[k]);
37         k-=k&(-k);
38     }
39     return re;
40 }
41 int n,c,d,b,p;
42 int an,tem;
43 char opt[10];
44 int main()
45 {
46     scanf("%d%d%d",&n,&c,&d);
47     while(n--)
48     {
49         scanf("%d%d%s",&b,&p,opt);
50         tem=0;
51         if(opt[0]=='C')
52         {
53             if(p>c)
54                 continue;
55             tem=read(D,d);
56             tem=max(tem,read(C,c-p));
57             add(C,p,b);
58         }
59         else
60         {
61             if(p>d)
62                 continue;
63             tem=read(C,c);
64             tem=max(tem,read(D,d-p));
65             add(D,p,b);
66         }
67         if(tem)
68             an=max(an,tem+b);
69     }
70     printf("%d\n",an);
71     return 0;
72 }
View Code

第一次写BIT,感觉比起线段树代码量的确少的多,但现在比较复杂的操作还不会用bit写……

转载于:https://www.cnblogs.com/quintessence/p/6852954.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值