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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void solve() {
		int n=s.nextInt();
		finaltime=s.nextInt();
		time_taken=new int[n];
		del= new int[n];
		for(int i =0;i<n;i++) {
			time_taken[i]=s.nextInt();
			del[i]=s.nextInt();
		}
		out.println(DP_solution());
	}
	
	
	public static int[] time_taken;
	public static int[] del;
	public static int finaltime;
 	public static long finalans =0;
	
 	
 	
 	
	public static long DP_solution() {
		int n=time_taken.length;
		long[][][] dp = new long[finaltime+1][n+1][2];
		for(int i = 0;i<=finaltime;i++) {
			for(int j=0;j<=n;j++) {
				dp[i][j][0]=-1;
				dp[i][j][1]=-1;
			}
		}
		dp[0][0][0]=0;
		for(int i=0;i<n;i++) {//object
			for(int j=0;j<2;j++) {//last
				for(int k= 0;k<finaltime;k++) {//time
					int curr_time=time_taken[i];
					int curr_del=del[i];
					if(dp[k][i][j]!=-1) {
						if(k+curr_time<finaltime) {
							dp[k+curr_time][i+1][j]=Long.max(dp[k+curr_time][i+1][j],dp[k][i][j]+curr_del);
						}
						dp[k][i+1][j]=Long.max(dp[k][i+1][j],dp[k][i][j]);
						if(j==0) {
							dp[k][i+1][1]=Long.max(dp[k][i][j]+curr_del,dp[k][i+1][1]);
						}
					}
				}
			}
		}
		long max= 0;
		for(int i=0;i<=n;i++) {
			for(int j=0;j<2;j++) {
				for(int k=0;k<=finaltime;k++) {
					max= Long.max(max,dp[k][i][j]);
				}
			}
		}
		return max;
	}
	
 	
// 	public static void Recur(int time,int pos ,boolean last,long ans) {
//		int n=time_taken.length;
//		if(pos == n) {
//			finalans=Long.max(ans, finalans);
//			return;
//		}
//		int curr_time=time_taken[pos];
//		int curr_del=del[pos];
//		if(last) {
//			if(time+curr_time<finaltime) {
//				Recur(time+curr_time,pos+1,last,ans+curr_del);
//			}
//			Recur(time,pos+1,last,ans);
//		}else {
//			if(time+curr_time<finaltime) {
//				Recur(time+curr_time,pos+1,last,ans+curr_del);
//			}
//			Recur(time,pos+1,last,ans);
//			Recur(time,pos+1,!last,ans+curr_del);
//			
//		}
	//}
	
	public static void main(String[] args) {
		out = new PrintWriter(new BufferedOutputStream(System.out));
		s = new FastReader();
		solve();
		out.close();
	}

	public static FastReader s;
	public static PrintWriter out;

	public static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	
}