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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Math;

namespace CSharpSpiral {
	class Program {
		static void Main(string[] args) {
			var sw = new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush=
#if DEBUG
			true
#else
			false
#endif
			};
			Console.SetOut(sw);
			new Program().Solve(new ConsoleInput(
#if DEBUG
			new StreamReader("../../../inputData.txt")
#else
			Console.In
#endif
			, ' '));
			Console.Out.Flush();
		}
		public void Solve(ConsoleInput cin) {
			var N = cin.ReadInt;
			var T = cin.ReadInt;
			var Table = new int[T+3001];
			for(int i = 0; i<N; i++) {
				var a = cin.ReadInt;
				var b = cin.ReadInt;
				for(int t = T-1; t>=0; t--) {
					Table[t+a]=Max(Table[t]+b, Table[t+a]);
				}
				for(int t = T+a; t<=T+3000; t++) {
					Table[t]=Table[t-1];
				}
			}
			Console.WriteLine(Table.Last());
		}
	}
	static class ExtendClass {
		public static void Swap<T>(this T[] A, int x, int y) {
			var tmp = A[x];
			A[x]=A[y];
			A[y]=tmp;
		}
		public static string Join<T>(this IEnumerable<T> X, string separator = " ") => string.Join(separator, X);
		public static IEnumerable<int> Indices<T>(this T[] X) => Enumerable.Range(0, X.Length);
		public static IEnumerable<int> Indices<T>(this T[,] X, int dim)
			=> Enumerable.Range(0, X.GetLength(dim));
		public static string Join<T>(this T[,] X, string separatorD2 = ",", string separatorD1 = "\n")
			=> X.Indices(0).Select(
				i => X.Indices(1).Select(j => X[i, j]).Join(separatorD2)
			).Join(separatorD1);
	}
	class ConsoleInput {
		private readonly TextReader _stream;
		private readonly char _separator = ' ';
		private readonly Queue<string> inputStream;
		public ConsoleInput(TextReader stream, char separator = ' ') {
			this._separator=separator;
			this._stream=stream;
			inputStream=new Queue<string>();
		}
		public string Read {
			get {
				if(inputStream.Count!=0) return inputStream.Dequeue();
				string[] tmp = _stream.ReadLine().Split(_separator);
				for(int i = 0; i<tmp.Length; ++i)
					inputStream.Enqueue(tmp[i]);
				return inputStream.Dequeue();
			}
		}
		public string ReadLine { get { return _stream.ReadLine(); } }
		public int ReadInt { get { return int.Parse(Read); } }
		public long ReadLong { get { return long.Parse(Read); } }
		public double ReadDouble { get { return double.Parse(Read); } }
		public string[] ReadStrArray(long N) {
			var ret = new string[N];
			for(long i = 0; i<N; ++i)
				ret[i]=Read;
			return ret;
		}
		public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i<N; ++i) ret[i]=ReadInt; return ret; }
		public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i<N; ++i) ret[i]=ReadLong; return ret; }
		public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i<N; ++i) ret[i]=ReadDouble; return ret; }
		public bool isEOF {
			get { return _stream.Peek()==-1; }
		}
	}
	class ResidueField {
		public readonly long modulo;
		public ResidueField(int mModulo) {
			modulo=mModulo;
		}
		private long[] factorialTable = { 1 };
		private int factorialTableLastIndex { get { return factorialTable.Length-1; } }
		public long mod(long x) => x<0 ? x%modulo+modulo : x%modulo;
		public long Add(long a, long b) => mod(a+b);
		public long Sub(long a, long b) => mod(a-b);
		public long Mul(long a, long b) => mod(a*b);
		public long Pow(long a, long n) {
			if(n==0) return 1;
			var R = 1L;
			n=n<0 ? n%(modulo-1)+(modulo-1) : n%(modulo-1);
			for(; n>0; a=Mul(a, a), n>>=1) {
				if((n&1)!=0) R=Mul(R, a);
			}
			return R;
		}
		private static long extgcd(long a, long b, ref long x, ref long y) {
			if(b==0) {
				x=1; y=0; return a;
			}
			var q = a/b;
			var g = extgcd(b, a-q*b, ref x, ref y);
			var z = x-q*y;
			x=y; y=z;
			return g;
		}
		public long Inv(long a) {
			if(a==0) throw new DivideByZeroException();
			long x = 0, y = 0;
			extgcd(a, modulo, ref x, ref y);
			return mod(x);
		}
		public long Div(long a, long b) => Mul(a, Inv(b));
		public void ExpandFactorialTable(int N) {
			var k = factorialTableLastIndex;
			Array.Resize(ref factorialTable, N+1);
			for(int i = k+1; i<=N; i++) {
				if(i==0) { factorialTable[i]=1; continue; }
				factorialTable[i]=Mul(factorialTable[i-1], i);
			}
		}
		public long Factorial(int n) {
			if(n>factorialTableLastIndex)
				ExpandFactorialTable(n);
			return factorialTable[n];
		}
		public long Permutation(int n, int k) {
			if(k<0||k>n) return 0;
			return Div(Factorial(n), Factorial(n-k));
		}
		public long Combination(int n, int k) {
			if(k<0||k>n) return 0;
			return Div(Factorial(n), Mul(Factorial(k), Factorial(n-k)));
		}
		public long Calculate(params object[] RPN) {
			var S = new Stack<long>();
			foreach(var token in RPN) {
				var type = token.GetType();
				if(type==typeof(int)||type==typeof(long)) {
					S.Push((long)token);
				} else if(type==typeof(char)) {
					long v1 = 0L, v2 = 0L;
					v2=S.Pop();
					HashSet<char> unaries = new HashSet<char>() { 'n', 'N', '!' };
					if(!unaries.Contains((char)token))
						v1=S.Pop();
					long res;
					switch((char)token) {
						case '+': res=Add(v1, v2); break;
						case '-': res=Sub(v1, v2); break;
						case '*': res=Mul(v1, v2); break;
						case '/': res=Div(v1, v2); break;
						case '^': res=Pow(v1, v2); break;
						case 'n': case 'N': res=modulo-v2; break;
						case '!': res=Factorial((int)v2); break;
						default: throw new InvalidOperationException();
					}
					S.Push(res);
				} else throw new InvalidOperationException();
			}
			return S.Peek();
		}
	}
}