Problem:
Score : 500 points

Problem Statement

Takahashi is at an all-you-can-eat restaurant.

The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th dish, whose deliciousness is B_i.

The restaurant has the following rules:

You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.

You cannot order the same kind of dish more than once.

Until you finish eating the dish already served, you cannot order a new dish.

After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.

Let Takahashi's happiness be the sum of the deliciousness of the dishes he eats in this restaurant.

What is the maximum possible happiness achieved by making optimal choices?

Constraints

2 \leq N \leq 3000

1 \leq T \leq 3000

1 \leq A_i \leq 3000

1 \leq B_i \leq 3000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N T
A_1 B_1
:
A_N B_N

Output

Print the maximum possible happiness Takahashi can achieve.

Sample Input 1

2 60
10 10
100 100

Sample Output 1

110

By ordering the first and second dishes in this order, Takahashi's happiness will be 110.

Note that, if we manage to order a dish in time, we can spend any amount of time to eat it.

Sample Input 2

3 60
10 10
10 20
10 30

Sample Output 2

60

Takahashi can eat all the dishes within 60 minutes.

Sample Input 3

3 60
30 10
30 20
30 30

Sample Output 3

50

By ordering the second and third dishes in this order, Takahashi's happiness will be 50.

We cannot order three dishes, in whatever order we place them.

Sample Input 4

10 100
15 23
20 18
13 17
24 12
18 29
19 27
23 21
18 20
27 15
22 25

Sample Output 4

145

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#![allow(non_snake_case)]
fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let N:usize = sc.read();
    let T:usize = sc.read();
    let mut A: Vec<usize> = vec![];
    let mut B:Vec<usize> = vec![];
    for _ in 0..N{
        let a:usize = sc.read();
        let b:usize = sc.read();
        A.push(a);
        B.push(b);
    }
    let mut ans:usize = 0;


    for j in 0..N{
    let mut dp: Vec< Vec<usize>> = vec![ vec![0; T]; N+1];
        for i in 0..N{
            if i == j{
                for t in 0..T{
                    dp[i+1][t] = dp[i][t];
                }
            }
            let a = A[i];
            let b = B[i];
            for t in 0..a{
                if t >= T{
                    break
                }
                dp[i+1][t] = dp[i][t];
            }
            for t in a..T{
                if dp[i][t] < dp[i][t-a]+b{
                    dp[i+1][t] = dp[i][t-a]+b;
                }else{
                    dp[i+1][t] = dp[i][t];
                }
            }
            
            // for t in T..(T+3000){
            //     if t < a{
            //         continue
            //     }
            //     if dp[i][t] < dp[i][t-a]+b{
            //         dp[i+1][t] = dp[i][t-a]+b;
            //     }else{
            //         dp[i+1][t] = dp[i][t];
            //     }            
            // }
        }
        if ans < dp[N][T-1] + B[j]{
            ans = dp[N][T-1] + B[j];
        }
    }
    // let mut ans:usize = 0;
    // for i in 0..(N+1){
    //     for j in 0..60{
    //         print!("{}", dp[i][j]);
    //     }
    //     println!("");
    // }
    // for i in 0..T{
    //     if ans < dp[N][i]{
    //         ans = dp[N][i];
    //     }
    // }
    // println!("{}", ans);
    // let mut plus:usize = 0;
    // let mut check:Vec<bool> = vec![false; N];
    // let mut a:usize = 0;
    // let mut b:usize = 0;
    // let mut dela:usize = 0;
    // let mut delb:usize = 0;


    // for i in 0..T{
    //     if dp[N][i] > b{
    //         dela = i - a;
    //         delb = dp[N][i] - b;
    //         a = i;
    //         b = dp[N][i];
    //     }
    //     if a == i{
    //         for i in 0..N{
    //             if !check[i]{
    //                 if (A[i] == dela) && (B[i] == delb){
    //                     check[i] = true;
    //                     break
    //                 }
    //             }
    //         }
    //     }
    // }
    // for i in 0..N{
    //     if !check[i]{
    //         if plus < B[i]{
    //             plus = B[i];
    //         }
    //     }
    // }
    
    println!("{}", ans);

}

pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
        .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}