Fountains (Codeforces 799C)
Tags: Implement, binary search

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.

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. 

Sample Inputs:
3 7 6
10 8 C
4 3 C
5 6 D

2 4 5
2 5 C
2 1 D

3 10 10
5 5 C
5 5 C
10 11 D

Sample Outputs:
9

0

10

Solution (Python):
import sys

def read_input(input_path=None):
    if input_path is None:
        f = sys.stdin
    else:
        f = open(input_path, 'r')
    (n, g, d) = map(int, f.readline().split())
    items = list()
    for _ in range(n):
        (power, money, type_) = f.readline().strip().split()
        items.append((int(power), int(money), type_))
    return (n, g, d, items)

def sol(n, g, d, items):
    gold_items = list()
    diamond_items = list()
    for item in items:
        if item[2] == 'D':
            if item[1] <= d:
                diamond_items.append((item[0], item[1]))
        elif item[1] <= g:
            gold_items.append((item[0], item[1]))
    gold_items = sorted(gold_items, key=lambda x: x[0], reverse=True)
    diamond_items = sorted(diamond_items, key=lambda x: x[0], reverse=True)
    mx = gold_items[0][0] + diamond_items[0][0] if gold_items and diamond_items else 0
    for (l, m) in ((gold_items, g), (diamond_items, d)):
        for i in range(len(l) - 1):
            (b1, p1) = l[i]
            if b1 * 2 <= mx:
                break
            for j in range(i + 1, len(l)):
                (b2, p2) = l[j]
                if b1 + b2 <= mx:
                    break
                if p1 + p2 <= m:
                    mx = b1 + b2
                    break
    return [f'{mx}']

def solve(input_path=None):
    return sol(*read_input(input_path))

def main():
    for line in sol(*read_input()):
        print(f'{line}')
if __name__ == '__main__':
    main()

Solution (Python):
import operator

def maxl(l, m, mx):
    for i in range(len(l) - 1):
        (b1, p1) = l[i]
        if b1 * 2 <= mx:
            break
        for j in range(i + 1, len(l)):
            (b2, p2) = l[j]
            if b1 + b2 <= mx:
                break
            if p1 + p2 <= m:
                mx = b1 + b2
                break
    return mx
fc = []
fd = []
(n, c, d) = map(int, input().split())
for _ in range(n):
    (b, p, m) = input().split()
    (b, p) = (int(b), int(p))
    if m == 'C':
        if p <= c:
            fc.append((b, p))
    elif p <= d:
        fd.append((b, p))
fc.sort(key=operator.itemgetter(0), reverse=True)
fd.sort(key=operator.itemgetter(0), reverse=True)
mx = fc[0][0] + fd[0][0] if fc and fd else 0
mx = maxl(fc, c, mx)
mx = maxl(fd, d, mx)
print(mx)

Solution (Python):
import operator

def maxl(l, m, mx):
    for i in range(len(l) - 1):
        (b1, p1) = l[i]
        if 2 * b1 <= mx:
            break
        for j in range(i + 1, len(l)):
            (b2, p2) = l[j]
            if b1 + b2 <= mx:
                break
            if p1 + p2 <= m:
                mx = b1 + b2
                break
    return mx
fc = []
fd = []
(n, c, d) = map(int, input().split())
for _ in range(n):
    (b, p, m) = input().split()
    (b, p) = (int(b), int(p))
    if m == 'C':
        if p <= c:
            fc.append((b, p))
    elif p <= d:
        fd.append((b, p))
fc.sort(key=operator.itemgetter(0), reverse=True)
fd.sort(key=operator.itemgetter(0), reverse=True)
mx = 0
if fc and fd:
    mx = fc[0][0] + fd[0][0]
mx = maxl(fc, c, mx)
mx = maxl(fd, d, mx)
print(mx)