原题链接;https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085
思路:01背包,不用说了。。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 1e4 + 5;
const double eps = 1e9;
typedef long long LL;
int w[MAXN], v[MAXN], dp[MAXN];
int main() {
int n, W;
scanf("%d %d", &n, &W);
for (int i = 0; i < n; i++) {
scanf("%d %d", &w[i], &v[i]);
}
for (int i = 0; i < n; i++) {
for (int j = W; j >= w[i]; j--) {
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
}
printf("%d\n", dp[W]);
return 0;
}