Source Code
n, X, Y = [int(d) for d in input().split()]
x_items, y_items, z_items = [], [], []
total = 0

for i in range(n):
    k, v = input().split()
    v = int(v)
    total += v
    if(k == 's'):
        x_items.append(v)
    if(k == 't'):
        y_items.append(v)
    if(k == 'c'):
        z_items.append(v)

def pre(ls):
    ls = sorted(ls)
    for i in range(1, len(ls)):
        ls[i] += ls[i-1]
    return ls

x_items, y_items, z_items = pre(x_items), pre(y_items), pre(z_items)
x_len, y_len, z_len = len(x_items), len(y_items), len(z_items)

def is_exhausted(x, y, z):
    return x + z < X or y + z < Y or x + y + z < X + Y

def gen_solutions():
            for z_y_side in range(z_len + 1):
                z_x_side = z_len - z_y_side
                groups = min((x_len + z_x_side) // X, (y_len + z_y_side) // Y)
                need_x_side = X * groups
                need_y_side = Y * groups
                x_taken = max(0, need_x_side - z_x_side)
                y_taken = max(0, need_y_side - z_y_side)
                x_left = x_len - x_taken
                y_left = y_len - y_taken
                z_used_x_side = min(z_x_side, need_x_side)
                z_used_y_side = min(z_y_side, need_y_side)
                z_left = z_len - z_used_x_side - z_used_y_side
                if not is_exhausted(x_left, y_left, z_left):
                    continue
                y_sum = y_items[y_taken - 1] if y_taken else 0
                z_sum = z_items[z_used_y_side - 1] if z_used_y_side else 0
                yield y_sum + z_sum

value = min(gen_solutions(), default=0)
print(total - value)
Copy
ClearTheBit Light
Python 3
1079 ms
33.7 MB
Time Limit Exceeded