from itertools import accumulate, product from math import inf import sys from math import inf from typing import Callable, Generic, Sequence, TypeVar def printe(*args, end="\n", **kwargs): print(*args, end=end, file=sys.stderr, **kwargs) def main(): N, W, D = map(int, input().split()) stones = [list(map(int, input().split())) for _ in range(N)] type_0_dp_table = [-inf for _ in range(W + 1)] type_1_dp_table = [-inf for _ in range(W + 1)] type_0_dp_table[0] = 0 type_1_dp_table[0] = 0 for idx in range(N): for c_w in reversed(range(W + 1)): if c_w + stones[idx][1] <= W: if stones[idx][0] == 0: type_0_dp_table[c_w + stones[idx][1]] = max( type_0_dp_table[c_w + stones[idx][1]], type_0_dp_table[c_w] + stones[idx][2] ) else: type_1_dp_table[c_w + stones[idx][1]] = max( type_1_dp_table[c_w + stones[idx][1]], type_1_dp_table[c_w] + stones[idx][2] ) max_value = 0 for type_0_w, type_1_w in product(range(W + 1), repeat=2): if type_0_w + type_1_w > W: continue if abs(type_0_w - type_1_w) > D: continue max_value = max( max_value, type_0_dp_table[type_0_w] + type_1_dp_table[type_1_w] ) print(max_value) if __name__ == "__main__": main()