Fountains
Contest: Playrix Codescapes Cup (Codeforces Round 413, rated, Div. 1 + Div. 2)
Difficulty rating: 1800
Tags: binary search, data structures, implementation

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 format:
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 format:
Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

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.

Example input:
3 7 6
10 8 C
4 3 C
5 6 D

Example output:
9

Example input:
2 4 5
2 5 C
2 1 D

Example output:
0

Editorial:
There are three ways — Arkady builds one fountain for coins and other for diamonds, Arkady builds both fountains for coins and Arkady builds both fountains for diamonds. In the end we need to choose a way with maximum total beauty.
The first way is simple — we need to choose one fountain of each type with maximum beauty on which is enough coins and diamonds which Arkady has. To make it we can easily iterate though all given fountains.
The other two ways are treated similarly to each other. Consider the way when Arkady builds both fountains for coins.
Let's write out all the fountains for a coins in a separate array. We will store them as pairs — cost and beauty. Then sort the array in ascending order of cost. Let Arkady will build fountains with cost p1 and p2 coins and p1 ≤ p2 and p1 + p2 ≤ c. Additionally we need an array maxB, where maxBi equals to maximum fountain beauty on the prefix of sorted fountains for coins which ends in position i.
The option when p1 equals to p2 should be considered separately. It can be done in one iteration through the fountains — from fountains with same cost we need to choose two with maximum beauty and update the answer with the resulting value.
It is only left case when p1 < p2. Let's brute the fountain with cost equals to p2. After that, Arcady will have c - p2 coins. After that with help of binary search we need to find the fountains prefix which Arkay can build for the remaining coins. With help of array maxB we can determine the maximum beauty of fountain which Arkady can build.
Similarly, the third way is solved when both fountains will be built for diamonds.