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: Runtime Error)

import java.util.*;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
	int N, T;
	List<Pair> dishes;
	Pair last;
	long maxScore = 0L;

	static class Pair {
		int A, B;

		Pair(String line) {
			String[] tokens = line.split(" ");
			this.A = Integer.parseInt(tokens[0]);
			this.B = Integer.parseInt(tokens[1]);
		}
	}

	long[][] dp;

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		this.N = Integer.parseInt(tokens[0]);
		this.T = Integer.parseInt(tokens[1]);
		this.dishes = new ArrayList<>(N);
		for (int i = 0; i < N; ++i) {
			this.dishes.add(new Pair(in.readLine()));
		}
		// 食べる時間が早い順でソートしておく
		// 最終的にDPで組み合わせを計算することから，最後に残るものは完食までの時間が長いものになる．
		Collections.sort(this.dishes, new Comparator<Pair>() {
			@Override
			public int compare(Pair o1, Pair o2) {
				return Integer.compare(o1.A, o2.A);
			}
		});
		calc();
	}

	void calc() {
		// dp[i][j]: 0-i番目の料理でj分いないに完食できるおいしさの合計の最大値
		// T + Amaxの枠をとる
		int tMax = T + this.dishes.get(this.dishes.size() - 1).A;
		// 皿をAの昇順で食べた時の完食時間を計算しておく
		long[] thresholds = new long[N];
		long[] sums = new long[N];
		thresholds[0] = this.dishes.get(0).A;
		sums[0] = this.dishes.get(0).B;
		for (int i = 1; i < N; ++i) {
			thresholds[i] = thresholds[i - 1] + this.dishes.get(i).A;
			sums[i] = sums[i - 1] + this.dishes.get(i).B;
		}

		this.dp = new long[N][tMax + 1];
		// 0分ではどの料理も完食できないので0とする
		for (int i = 0; i < N; ++i) {
			this.dp[i][0] = 0L;
		}
		// 1つ目の料理だけを食べる場合の初期値を設定する
		Arrays.fill(this.dp[0], 0L);
		// dishes[0].A以上なら完食できる
		Arrays.fill(this.dp[0], this.dishes.get(0).A, T, (long) (this.dishes.get(0).B));
		for (int i = 1; i < N; ++i) {
			Pair dish = this.dishes.get(i);
			for (int j = 1; j <= tMax; ++j) {
				if (j > thresholds[i]) {
					// 全部食べられた
					this.dp[i][j] = sums[i];
				} else {
					int time = j - dish.A;
					if (time < 0) {
						// この皿を加える余地はない
						this.dp[i][j] = Math.max(this.dp[i - 1][j], this.dp[i][j - 1]);
					} else {
						// この皿を加えない場合と比べる
						this.dp[i][j] = Math.max(Math.max(this.dp[i - 1][j], this.dp[i][j - 1]),
								this.dp[i - 1][time] + dish.B);
					}
				}
			}
		}
		// T + A[i]の値を見て，最大のものが解
		long max = 0L;
		for (int i = 0; i < N; ++i) {
			int time = T + this.dishes.get(i).A - 3;
			max = Math.max(max, this.dp[i][time]);
		}
		this.maxScore = max;
	}

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		System.out.println(ins.maxScore);
	}

}