import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto A = iota(N).map!(_ => readln.split.map!(to!int).array).array; A.sort!"a[0] > b[0]"; auto dp = new int[][][](2, M+1, 2); foreach (i; 0..2) foreach (j; 0..M+1) dp[i][j][1] = -(1 << 29); int cur = 0; int tar = 1; foreach (i; 0..N) { int p = A[i][0]; int d = A[i][1]; foreach (j; 0..M+1) { dp[tar][j][0] = 0; dp[tar][j][1] = -(1 << 29); } foreach (j; 0..M+1) { dp[tar][j][0] = max(dp[tar][j][0], dp[cur][j][0]); dp[tar][j][1] = max(dp[tar][j][1], dp[cur][j][1]); if (p + j <= M) dp[tar][j+p][1] = max(dp[tar][j+p][1], dp[cur][j][0] + d); dp[tar][j][0] = max(dp[tar][j][0], dp[cur][j][1] + d); } swap(cur, tar); } int ans = 0; foreach (j; 0..M+1) foreach (k; 0..2) ans = max(ans, dp[cur][j][k]); ans.writeln; }