总结:主要是熟悉一维数组写01背包的用法,可以节约不少空间复杂度。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 100005;
int dp[MAX];
int W[MAX], D[MAX];
int main()
{
int M, N, i, j;
scanf("%d%d", &N, &M);
memset(dp, 0, sizeof(dp));
for (i = 0; i < N; i++)
{
scanf("%d%d", &W[i], &D[i]);
}
for(i = 0;i<N;i++)
for (j = M; j >= W[i]; j--)
{
dp[j] = max(dp[j], dp[j - W[i]] + D[i]);
}
cout << dp[M] << endl;
return 0;
}