#![allow(non_snake_case)] use proconio::{input}; fn main() { input!{ n: usize, W: usize, D: usize, items: [(usize, usize, i32); n] } let mut value = vec![vec![i32::MIN; W + 1]; 2]; value[0][0] = 0; value[1][0] = 0; for (t, w, v) in items { if w > W { continue; } for x in (0..=(W - w)).rev() { value[t][x + w] = value[t][x + w].max(value[t][x] + v); } } let mut ans = 0; for x in 0..=W { for y in 0..=(W - x) { if x.abs_diff(y) <= D && value[0][x] >= 0 && value[1][y] >= 0 { ans = ans.max(value[0][x] + value[1][y]); } } } println!("{ans}"); }