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.Linq;
using System.Text;

using System.Runtime.CompilerServices;
using ll=System.Int64;

public class Contest
{
	static void Swap1<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
	static void Swap2<T>(ref T a, ref T b) where T : IComparable { if (a.CompareTo(b)==1) Swap1(ref a, ref b); }  // b の方が小さければ交換
	static bool Chmin<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)== 1) { a = b; return true; } return false; }  // b の方が小さければ a を更新
	static bool Chmax<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)==-1) { a = b; return true; } return false; }  // b の方が大きければ a を更新
	static T[][] InitMat<T>(long len1, long len2, T val){ T[][] m = new T[len1][]; for (int i = 0; i < m.Length; i++) m[i] = Enumerable.Repeat(val, (int)len2).ToArray(); return m; }
	static T[][][] InitMat<T>(long len1, long len2, long len3, T val){ T[][][] m = new T[len1][][]; for (int i = 0; i < m.Length; i++) m[i] = InitMat(len2,len3,val); return m; }
	public const long MOD1 = 1000000007; // 10^9+7
	public const double EPS1 = 1e-8;

	public static void Main() {
		Solve_E();
 	}

	public static void Solve_A() {
		ll  n;
		MyIO.ReadMulti(out n);



		ll ans = n*n;

		MyIO.WriteMulti(ans);
	}
	public static void Solve_B() {
		ll  n;
		MyIO.ReadMulti(out n);
		string s;
		MyIO.ReadMulti(out s);

		string ans="No";
		if(n%2==0){
			string a = s.Substring(0,(int)(n/2));
			string b = s.Substring((int)(n/2),(int)(n/2));
			if(a==b) ans="Yes";
		}



		MyIO.WriteMulti(ans);
	}
	public static void Solve_C() {
		ll  n;
		MyIO.ReadMulti(out n);
		VT<ll,ll>[] xyl;
		MyIO.ReadLines(n, out xyl);

		ll[][] dist = InitMat(n,n,0L);
		double sum = 0;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				sum += Math.Sqrt((xyl[i].Item1-xyl[j].Item1)*(xyl[i].Item1-xyl[j].Item1)+(xyl[i].Item2-xyl[j].Item2)*(xyl[i].Item2-xyl[j].Item2));
			}
		}


		double ans = (double)sum/(double)n;

		MyIO.WriteMulti(ans);
	}
	public static void Solve_D() {
		ll  x, y;
		MyIO.ReadMulti(out x, out y);

		ll mx = x+y;
		ll my = x-y;

		ll ans = 0;
		if(mx%3==0){
			mx/=3;
			my += mx;
			if((my%2==0)&&(0<=my)&&(my<=2*mx)){
				my/=2;
				ModCombination mc = new ModCombination(mx+1,MOD1);
				ans = mc.C(mx,my).Value;
			}



		}


		MyIO.WriteMulti(ans);
	}
	public static void Solve_E() {
		ll  n, t;
		MyIO.ReadMulti(out n, out t);
		VT<ll,ll>[] abl;
		MyIO.ReadLines(n, out abl);
		Array.Sort(abl,(x,y)=>(int)(x.Item2-y.Item2));
		ll[][] dp = InitMat(n+1,t+1,0L);
		if(abl[0].Item1<=t) dp[0][abl[0].Item1] = abl[0].Item2;
		for (int i = 1; i < n-1; i++)
		{
			for (int j = 0; j <= t; j++)
			{
				dp[i][j] = Math.Max(dp[i-1][j], ((0<=j-abl[i].Item1)?(dp[i-1][(int)(j-abl[i].Item1)]+abl[i].Item2):0)        );
			}
		}

		ll ans = 0;
		for (int j = 0; j < t-1; j++)
		{
			Chmax(ref ans, dp[n-2][j]);
		}
		ans += abl[n-1].Item2;

		MyIO.WriteMulti(ans);
	}
	public static void Solve_F() {

	}


















}





public struct ModInt {
	public long Value;
	public long Modulo;

	public ModInt(long value) : this(value, long.MaxValue) {}
	public ModInt(long value, long modulo) {
		this.Modulo = modulo;
		this.Value = value >= 0 ? value % modulo : (modulo - (-value) % modulo) % modulo;
	}

    public static implicit operator ModInt(long v) { return new ModInt(v, 1000000007L); }
	public static ModInt operator-(ModInt a) { return new ModInt(-1*a.Value, a.Modulo); }
	public static ModInt operator+(ModInt a, ModInt b) { return new ModInt(a.Value + b.Value, a.Modulo); }
	public static ModInt operator-(ModInt a, ModInt b) { return new ModInt(a.Value - b.Value, a.Modulo); }
	public static ModInt operator*(ModInt a, ModInt b) { return new ModInt(a.Value * b.Value, a.Modulo); }
	public static ModInt operator/(ModInt a, ModInt b) { return a * b.Inverse(); }
	public static bool operator==(ModInt a, ModInt b) { return a.Equals(b); }
	public static bool operator!=(ModInt a, ModInt b) { return !a.Equals(b); }

	public ModInt Inverse() {
		long a = this.Value, b = this.Modulo, u = 1, v = 0, t, s;
		while(b > 0) {
			t = a / b;
			s = a - t * b; a = b; b = s;
			s = u - t * v; u = v; v = s;
		}
		return new ModInt(u, this.Modulo);
	}

	public ModInt Pow(long n) {
		ModInt ret = new ModInt(1, this.Modulo);
		ModInt mul = new ModInt(this.Value, this.Modulo);
		while(n > 0) {
			if((n & 1)==1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}

    public override bool Equals(object obj) {
        if (!(obj is ModInt)) throw new ArgumentException("Object is not a ModInt");
        return Equals((ModInt)obj);   
    }
    public bool Equals(ModInt p) => (this.Value == p.Value);
    public override int GetHashCode() => (int)(this.Value ^ this.Modulo);
	public override string ToString() => this.Value.ToString();

}

// nPr nCr nHr B(n,k) S{nk} P(n,k) を求める。
// ModCombinationは結果をmod     ※ ModInt クラスが必要
// ベル数 B(n,k)
//   区別できる n個のボールを区別できない k個以下の箱に分割する方法の数を与える。
//   特に B(n,n)は n 個のボールを任意個のグループに分割する方法の数である。
// 第2種スターリング数 S{nk}
//   区別できる n 個のボールを区別できない k 個の箱に分割する方法の数を与える。
// 分割数テーブル
//   分割数 P(n,k) は整数 n をちょうど k 個の非負整数の和で表す方法の数を与える。
//   順序が異なるものは同一視する。
public class ModCombination {
	private ModInt[] _fact, _rfact, _inv;
	private ModInt[] _mm;

	public ModCombination(long sz) : this(sz, long.MaxValue) {}
	public ModCombination(long sz, long modulo){
		_fact = new ModInt[sz+1];
		_rfact = new ModInt[sz+1];
		_inv = new ModInt[sz+1];

		_mm = new ModInt[]{new ModInt(0,modulo), new ModInt(1,modulo)};
		
		_fact[0] = _rfact[sz] = _inv[0] = _mm[1];
		for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
		_rfact[sz] /= _fact[sz];
		for(int i = (int)sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
		for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
	}

	public ModInt P(long n, long r) {
		if(r < 0 || n < r) return _mm[0];
		return _fact[n] * _rfact[n - r];
	}

	public ModInt C(long n, long r) {
		if(r < 0 || n < r) return _mm[0];
		return _fact[n] * _rfact[r] * _rfact[n - r];
	}

	public ModInt H(long n, long r) {
		if(n < 0 || r < 0) return _mm[0];
		return r == 0 ? _mm[1] : C(n + r - 1, r);
	}
	
	public ModInt B(long n, long k) {
		if(n == 0) return _mm[1];
		k = k<n ? k : n;
		ModInt ret = _mm[0];
		long mo = _mm[0].Modulo;
		ModInt[] pref = new ModInt[k + 1];
		pref[0] = _mm[1];
		for(int i = 1; i <= k; i++) {
			if((i & 1)==1) pref[i] = pref[i - 1] - _rfact[i];
			else pref[i] = pref[i - 1] + _rfact[i];
		}
		for(int i = 1; i <= k; i++) ret += (new ModInt(i, mo)).Pow(n) * _rfact[i] * pref[k - i];

		return ret;
	}
	
	public ModInt S(long n, long k) {
		ModInt ret = _mm[0];
		long mo = _mm[0].Modulo;
		for(int i = 0; i <= k; i++) {
			var add = (new ModInt(i, mo)).Pow(n) * this.C(k, i);
			if(((k - i) & 1)==1) ret -= add;
			else ret += add;
		}
		return ret * _rfact[k];
	}

	public static ModInt[][] PartitionTable(long n, long k, long modulo=long.MaxValue) {
		ModInt[][] dp = new ModInt[n + 1][];
		for(int i=0;i<=n;i++) dp[i] = new ModInt[k + 1];
		dp[0][0] = new ModInt(1, modulo);
		for(int i = 0; i <= n; i++) {
			for(int j = 1; j <= k; j++) {
				if(i - j >= 0) dp[i][j] = dp[i][j - 1] + dp[i - j][j];
				else dp[i][j] = dp[i][j - 1];
			}
		}
		return dp;
	}


}











#region MyIO

public struct VT<T, U>
{
	public T Item1; public U Item2;
    public VT(T a, U b) { Item1 = a; Item2 = b; }
}
public struct VT<T, U, V>
{
	public T Item1; public U Item2; public V Item3;
    public VT(T a, U b, V c) { Item1 = a; Item2 = b; Item3 = c; }
}
public struct VT<T, U, V, W>
{
	public T Item1; public U Item2; public V Item3; public W Item4;
    public VT(T a, U b, V c, W d) { Item1 = a; Item2 = b; Item3 = c; Item4 = d; }
}

public class Printer : System.IO.StreamWriter
{
	public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
	public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
	public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
}
public static class MyIO
{
	private static char[] splitter = new char[] {' '};
	private static bool eq<T, U>() => typeof(T).Equals(typeof(U));
	private static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
	private static T cv<T>(string s) =>
		eq<T, int>()    ? ct<T, int>(int.Parse(s))
	  : eq<T, long>()   ? ct<T, long>(long.Parse(s))
	  : eq<T, double>() ? ct<T, double>(double.Parse(s, System.Globalization.CultureInfo.InvariantCulture))
	  : eq<T, char>()   ? ct<T, char>(s[0])
						: ct<T, string>(s);
			
	public static string[] ReadMulti<T>(out T a) {
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(ar[0]);
		return ar;
	}
	public static string[] ReadMulti<T, U>(out T a, out U b) {
		var ar = ReadMulti<T>(out a); 
        b = cv<U>(ar[1]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V>(out T a, out U b, out V c) {
		var ar = ReadMulti<T, U>(out a, out b); 
        c = cv<V>(ar[2]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W>(out T a, out U b, out V c, out W d) {
		var ar = ReadMulti<T, U, V>(out a, out b, out c); 
        d = cv<W>(ar[3]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) {
		var ar = ReadMulti<T, U, V, W>(out a, out b, out c, out d); 
        e = cv<X>(ar[4]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) {
		var ar = ReadMulti<T, U, V, W, X>(out a, out b, out c, out d, out e); 
        f = cv<Y>(ar[5]);
		return ar;
	}
	public static string[] ReadArray<T>(out T[] a) {		
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries);
		a = ar.Select(cv<T>).ToArray();
		return ar;
	}		
	public static string[][] ReadLines<T>(long n, out T[] a) {
		a = new T[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadMulti(out a[i]);
		return arr;
	}
	public static string[][] ReadLines<T, U>(long n, out VT<T, U>[] a) {
		a = new VT<T, U>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p;
			arr[i] = ReadMulti(out o, out p);
			a[i] = new VT<T, U>(o, p);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V>(long n, out VT<T, U, V>[] a) {
		a = new VT<T, U, V>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q;
			arr[i] = ReadMulti(out o, out p, out q);
			a[i] = new VT<T, U, V>(o, p, q);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V, W>(long n, out VT<T, U, V, W>[] a) {
		a = new VT<T, U, V, W>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q; W r;
			arr[i] = ReadMulti(out o, out p, out q, out r);
			a[i] = new VT<T, U, V, W>(o, p, q, r);
		}
		return arr;
	}
	public static string[][] ReadMatrix<T>(long n, out T[][] a) {
		a = new T[n][];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadArray(out a[i]);
		return arr;
	}

	private static void wwp(Action act){
		var sw = new Printer(Console.OpenStandardOutput()){AutoFlush = false};
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	private static string wfm(Type t) =>t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	public static void WriteMulti(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length;i++) {
				string fs = wfm(a[i].GetType()) + ((i<a.Length-1) ? " " : Environment.NewLine);
				Console.Write(fs, a[i]);
			}
		});
	}
	public static void WriteArray<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T));
			string fs1 = fs + " ";
			string fs2 = fs + Environment.NewLine;
			for(int i=0;i<a.Length;i++) Console.Write( ((i<a.Length-1) ? fs1 : fs2), a[i]);
		});
	}
	public static void WriteLines<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T)) + Environment.NewLine;
			foreach(T x in a) Console.Write(fs, x);
		});
	}


}

#endregion