题目:
![](https://i-blog.csdnimg.cn/blog_migrate/ab4580d841c79b5bfe254f7ba582d477.png)
代码(首刷看解析):
#include<bits/stdc++.h>
using namespace std;
int help(int N, int bagSize, vector<vector<int>>& materals) {
vector<int> dp(bagSize + 1, 0);
for (int i = 0; i < N; ++i) {
for (int j = materals[i][0]; j <= bagSize; ++j) {
dp[j] = max(dp[j], dp[j - materals[i][0]] + materals[i][1]);
}
}
return dp[bagSize];
}
int main() {
int N, V;
cin>>N>>V;
vector<vector<int>> test(N + 1, vector<int>(2,0));
for (int i = 0; i < N; ++i) {
int w;
int v;
cin>>w>>v;
test[i][0] = w;
test[i][1] = v;
}
int res = help(N, V, test);
cout<<res<<endl;
return 0;
}