N,W=map(int, input().split()) data=[] for _ in range(N): weight,cost=map(int, input().split()) data.append((weight,cost)) data.sort(key=lambda x:x[0],reverse=True) dp=[0]*(W+1) dp2=[0]*(W+1) for weight, cost in data: ndp=dp[:] ndp2=dp2[:] for w in range(W+1)[::-1]: if dp2[w]!=0: ndp[w]=max(dp2[w]+cost,ndp[w]) if w==weight: ndp2[w]=max(ndp2[w],cost) ndp[w]=max(ndp[w],cost) if w>weight and dp[w-weight]!=0: ndp2[w]=max(ndp2[w],dp[w-weight]+cost) ndp[w]=max(ndp[w],dp[w-weight]+cost) dp=ndp dp2=ndp2 print(max(max(dp),max(dp2)))