#include #include #include using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N, K; cin >> N >> K; vector P(N), D(N), perm(N); for (int i = 0; i < N; ++i) { cin >> P[i] >> D[i]; perm[i] = i; } sort(perm.begin(), perm.end(), [&](int i, int j) { return P[i] > P[j]; }); vector dp0(K + 1), dp1(K + 1, -(1 << 30)); for (int i = 0; i < N; ++i) { vector ndp0 = dp0, ndp1 = dp1; for (int j = P[perm[i]]; j <= K; ++j) { ndp1[j] = max(ndp1[j], dp0[j - P[perm[i]]] + D[perm[i]]); } for (int j = 0; j <= K; ++j) { ndp0[j] = max(ndp0[j], dp1[j] + D[perm[i]]); } dp0 = ndp0; dp1 = ndp1; } cout << max(dp0[K], dp1[K]) << endl; return 0; }