use std::str::FromStr; use std::{io::*, vec}; fn main() { let n: usize = read(); let w: usize = read(); let d: usize = read(); let mut value = vec![vec![i32::MIN; w + 1]; 2]; value[0][0] = 0; value[1][0] = 0; for _i in 1..=n { let ti: usize = read(); let wi: usize = read(); let vi: usize = read(); if wi > w{ continue; } for x in (0..=(w - wi)).rev(){ value[ti][x + wi] = value[ti][x + wi].max(value[ti][x] + vi as i32); } } 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); } pub fn read() -> T { let stdin = stdin(); let stdin = stdin.lock(); let token: String = stdin .bytes() .map(|c| c.expect("failed to read char") as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect(); token.parse().ok().expect("failed to parse token") }