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[][][](N+1, M+1, 2); foreach (i; 0..N+1) foreach (j; 0..M+1) dp[i][j][1] = -(1 << 29); foreach (i; 0..N) foreach (j; 0..M+1) { int p = A[i][0]; int d = A[i][1]; dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][0]); dp[i+1][j][1] = max(dp[i+1][j][1], dp[i][j][1]); if (p + j <= M) dp[i+1][j+p][1] = max(dp[i+1][j+p][1], dp[i][j][0] + d); dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][1] + d); } int ans = 0; foreach (j; 0..M+1) foreach (k; 0..2) ans = max(ans, dp[N][j][k]); ans.writeln; }