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++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
#define rep(i, n) REP(i, 0, n)
#define REP(i, l, r) for (int i = l; i < r; ++i)
#define int long long
#ifdef DEBUG
#define dout cout
#else
std::stringstream dout;
#endif
using namespace std;
typedef pair<int, int> P;
struct edge {
	int to, cost;
};
// other libs
int gcd(int a, int b) {
	if (a % b == 0)
		return b;
	else
		return gcd(b, a % b);
}
int lcm(int a, int b) {
	return a * b / gcd(a, b);
}

class dijkstra {
  private:
	int V;
	int d[100002];
	priority_queue<P, vector<P>, greater<>> que;

  public:
	dijkstra(int v, int s, vector<edge> G[]) {
		V = v;
		update(s, G);
	}

	void update(int s, vector<edge> G[]) {
		fill(d, d + V + 1, 100000000000ll);
		d[s] = 0;
		que.push(make_pair(0, s));
		while (!que.empty()) {
			P p = que.top();
			que.pop();
			int v = p.second;
			if (d[v] < p.first) continue;
			rep(i, G[v].size()) {
				edge e = G[v][i];
				if (d[e.to] > d[v] + e.cost) {
					d[e.to] = d[v] + e.cost;
					que.push(make_pair(d[e.to], e.to));
				}
			}
		}
	}

	int find(int e) {
		return d[e];
	}
};
template<typename T>
class Union_Find {
  private:
	vector<T> t, s;

  public:
	Union_Find(T max_length) {
		rep(i, max_length + 1) {
			t.push_back(i);
			s.push_back(1);
		}
	}

	void Union(T x, T y) {
		if (same(x, y)) return;
		T tx = Find(x), ty = Find(y);
		if (s[tx] < s[ty]) {
			s[ty] += s[tx];
			t[tx] = ty;
		}
		else if (s[tx] > s[ty]) {
			s[tx] += s[ty];
			t[ty] = tx;
		}
		else if (tx > ty) {
			t[tx] = ty;
			s[ty] += s[tx];
		}
		else {
			t[ty] = tx;
			s[tx] += s[ty];
		}
	}
	T Find(T n) {
		if (t[n] == n)
			return n;
		else
			return t[n] = Find(t[n]);
	}

	bool same(T x, T y) {
		return Find(x) == Find(y);
	}

	T get_Size(size_t a) {
		return s[a];
	}
};
bool ifPrime(int n) {
	if (n == 1) return false;
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) return false;
	}
	return true;
}

set<int> prime_num_factor(int n) {
	if (n < 2) return set<int>();
	set<int> prime;
	prime.insert(2);
	for (int i = 3; i <= n; i++) {
		for (int it : prime) {
			if (it * it > i) {
				prime.insert(i);
				break;
			}
			if (i % it == 0) break;
		}
	}
	return prime;
}
vector<int> makeDivisors(int n) {
	vector<int> re;
	REP(i, 1, sqrt(n) + 1) {
		if (n % i == 0) {
			re.push_back(i);
			if (i != n / i) re.push_back(n / i);
		}
	}
	return re;
}
int modPow(int a, int n, int mod) {
	int ret = 1;
	while (n) {
		if (n & 1) ret = ret * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return ret;
}
int comb(int n, int r) {
	if (n - r < r) r = n - r;
	if (r == 0) return 1;
	if (r == 1) return n;

	vector<int> num(r);
	vector<int> den(r);

	for (int k = 0; k < r; k++) {
		num[k] = n - r + k + 1;
		den[k] = k + 1;
	}

	for (int p = 2; p <= r; p++) {
		int pivot = den[p - 1];
		if (pivot > 1) {
			int offset = (n - r) % p;
			for (int k = p - 1; k < r; k += p) {
				num[k - offset] /= pivot;
				den[k] /= pivot;
			}
		}
	}

	int ret = 1;
	for (int k = 0; k < r; k++) {
		if (num[k] > 1) ret = ret * num[k] % 1000000007;
	}

	return ret;
}
signed main(signed argc, char* argv[]) {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, t;
	cin >> n >> t;
	vector<P> f(n);
	rep(i, n) cin >> f[i].first >> f[i].second;
	sort(f.begin(), f.end());
	vector<vector<int>> dp(n + 1, vector<int>(t + 3000 + 1, 0));
	REP(i, 1, n + 1) {
		rep(j, t + 1 + 3000) {
			if (j - f[i - 1].first < 0) {
				dp[i][j] = dp[i - 1][j];
			}
			else {
				dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - f[i - 1].first] + f[i - 1].second);
			}
		}
	}
	int ans = dp[n][t - 1];
	REP(j, t + 1, t + 1 + 3000) {
		int i = n - 1;
		while(true){
		    if(j >= f[i].first&&dp[n][j] == dp[i][j-f[i].first] + f[i].second){
		        if(j-f[i].first<t)ans=max(ans,dp[n][j]);
		        break;
		    }
		    i--;
		}
	}
	cout << ans << endl;
}