local mmi, mma = math.min, math.max
local n, t = io.read("*n", "*n")
local w, v = {}, {}
for i = 1, n do
  w[i], v[i] = io.read("*n", "*n")
end
local dp = {}
for i = 1, n - 1 do
  dp[i] = {}
  for j = 1, t do
    dp[i][j] = 0
  end
end
do
  for j = 1 + w[1], t do
    dp[1][j] = v[1]
  end
end
for i = 2, n - 1 do
  for j = 1, t do
    dp[i][j] = dp[i - 1][j]
  end
  local wi, vi = w[i], v[i]
  for j = 1, t - wi do
    dp[i][j + wi] = mma(dp[i][j + wi], dp[i - 1][j] + vi)
  end
end
local ret = dp[n - 1][t] + v[n]
local backtbl = {}
for i = 1, t do
  backtbl[i] = 0
end
for i = n, 2, -1 do
  local wi, vi = w[i], v[i]
  for j = t - wi, 1, -1 do
    backtbl[j + wi] = mma(backtbl[j + wi], backtbl[j] + vi)
  end
  if 2 < i then
    local cand = 0
    for j = 1, t do
      cand = mma(cand, dp[i - 2][j] + backtbl[t + 1 - j])
    end
    ret = mma(ret, cand + v[i - 1])
  else
    ret = mma(ret, v[1] + backtbl[t])
  end
end
print(ret)
