典型的0-1背包问题
#include<iostream>
using namespace std;
const int SIZE=40000;
int weight[SIZE],value[SIZE],DP[SIZE];
int main()
{
int N,M,i,j;
cin>>N>>M;
memset(weight,0,sizeof(weight));
memset(value,0,sizeof(value));
memset(DP,0,sizeof(DP));
for(i=0;i<N;i++)
cin>>weight[i]>>value[i];
for(i=0;i<N;i++)
for(j=M;j>=weight[i];j--)
if(DP[j-weight[i]]+value[i]>DP[j])
DP[j]=DP[j-weight[i]]+value[i];
cout<<DP[M]<<endl;
return 0;
}