use std::io::Read; fn run() { let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace(); let n: usize = it.next().unwrap().parse().unwrap(); let w: usize = it.next().unwrap().parse().unwrap(); let mut p: Vec<(usize, i32)> = (0..n).map(|_| { let p: usize = it.next().unwrap().parse().unwrap(); let d: i32 = it.next().unwrap().parse().unwrap(); (p, d) }).collect(); p.sort(); let mut now = vec![vec![std::i32::MIN; w + 1]; 2]; now[0] = vec![0; w + 1]; let mut next = now.clone(); for (p, d) in p.into_iter().rev() { next.clone_from(&now); for (next, now) in next[1][p..].iter_mut().zip(now[0].iter()) { *next = std::cmp::max(*next, *now + d); } for (next, now) in next[0].iter_mut().zip(now[1].iter()) { *next = std::cmp::max(*next, *now + d); } std::mem::swap(&mut now, &mut next); } let ans = std::cmp::max(now[0][w], now[1][w]); println!("{}", ans); } fn main() { run(); }