inputs: I found an interesting problem on https://codeforces.com/problemset/problem/799/C:
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 b_{i} and p_{i} (1 ≤ b_{i}, p_{i} ≤ 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.

I tried it in Python, but could not do it. Can you solve it?
targets: from operator import itemgetter


def get_one_max(arr, resource):
    res = 0
    for beauty, _ in [x for x in arr if x[1] <= resource]:
        res = max(res, beauty)
    return res


def get_two_max(arr, resource):
    arr.sort(key=itemgetter(1))
    best = [-1] * (resource + 1)
    ptr = 0
    for index, (beauty, price) in enumerate(arr):
        if price > resource:
            break
        while ptr < price:
            ptr += 1
            best[ptr] = best[ptr - 1]
        if best[ptr] == -1 or arr[best[ptr]][0] < beauty:
            best[ptr] = index

    while ptr < resource:
        ptr += 1
        best[ptr] = best[ptr - 1]

    res = 0
    for index, (beauty, price) in enumerate(arr):
        rest = resource - price
        if rest <= 0:
            break
        if best[rest] == -1 or best[rest] == index:
            continue
        res = max(res, beauty + arr[best[rest]][0])

    return res


n, coins, diamonds = list(map(int, input().split()))

with_coins = []
with_diamonds = []

for i in range(n):
    beauty, price, tp = input().split()
    if tp == 'C':
        with_coins.append((int(beauty), int(price)))
    else:
        with_diamonds.append((int(beauty), int(price)))

with_coins_max = get_one_max(with_coins, coins)
with_diamonds_max = get_one_max(with_diamonds, diamonds)
ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 \
    else with_coins_max + with_diamonds_max
ans = max(ans, get_two_max(with_coins, coins))
ans = max(ans, get_two_max(with_diamonds, diamonds))

print(ans)