0-1背包
作者:
殇ベ_11
,
2021-02-03 15:47:52
,
所有人可见
,
阅读 293
#include<iostream>
using namespace std;
const int N = 100000;
int dp[N << 1],we[N << 1],val[N << 1],cnt,ans,n,m;
int read()
{
char c = getchar();
int f = 1, x = 0;
while(!isdigit(c)) {if(c == '-') f = -1;c = getchar();}
while(isdigit(c)){x = x * 10 + c - 48;c = getchar();}
return x * f;
}
int main()
{
n = read();
m = read();
for(int i=1;i<=n;i++)
{
we[i] = read();
val[i] = read();
}
for(int i=1;i<=n;i++)
{
for(int j=m;j>=we[i];j--) //注意从大到小
{
dp[j]=max(dp[j],dp[j-we[i]]+val[i]);
}
}
cout<<dp[m]<<endl;
return 0;
}