#include "bits/stdc++.h" using namespace std; #define int long long #define FOR(i, a, b) for(int i=(a);i<(b);i++) #define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--) #define REP(i, n) for(int i=0; i<(n); i++) #define RREP(i, n) for(int i=(n-1); i>=0; i--) #define ALL(a) (a).begin(),(a).end() #define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end()); #define CONTAIN(a, b) find(ALL(a), (b)) != (a).end() #define array2(type, x, y) array, x> #define vector2(type) vector > #define out(...) printf(__VA_ARGS__) #define MAX LONG_LONG_MAX int dxy[] = {0, 1, 0, -1, 0}; /*================================*/ int N, K; struct P { int score; int price; }; signed main() { #if DEBUG std::ifstream in("input.txt"); std::cin.rdbuf(in.rdbuf()); #endif cin >> N >> K; vector

LIST(N); REP(i, N) { cin >> LIST[i].price >> LIST[i].score; } sort(ALL(LIST), [](const P a, const P b) {return a.price > b.price || (a.price == b.price && a.score > b.score); }); int DP[N][K+1][2]; REP(i, N) REP(j, K+1) REP(k, 2) DP[i][j][k] = -1; DP[0][K][0] = 0; P p = LIST[0]; if (K >= p.price) { DP[0][K - p.price][1] = p.score; } FOR(i, 1, N) REP(j, K+1) REP(k, 2) { P p = LIST[i]; if (DP[i-1][j][k] > -1) { if (j >= p.price) DP[i][j - p.price][1] = max(DP[i][j - p.price][1], DP[i-1][j][k] + p.score); // 買った場合 if (k == 1) DP[i][j][0] = max(DP[i][j][0], DP[i-1][j][k] + p.score); // 特典を利用した場合 DP[i][j][k] = max(DP[i][j][k], DP[i-1][j][k]); // 買わなかった場合 } } int ans = 0; REP(i, N) REP(j, K+1) REP(k, 2) { ans = max(ans, DP[i][j][k]); } cout << ans << endl; return 0; }