#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100; //物品最大件数
const int maxv = 1000; //V的上限
int w[maxn], c[maxn], dp[maxv];
int main(){
int n, V;
scanf("%d%d", &n, &V);
for(int i = 1; i <= n; i++){
scanf("%d", &w[i]);
}
for(int i = 1; i <= n; i++){
scanf("%d", &c[i]);
}
//边界
for(int v = 0; v <= V; v++){
dp[v] = 0;
}
for(int i = 1; i <= n; i++){
for(int v = w[i]; v <= V; v++){ //和01背包问题唯一区别:正向枚举
//状态转移方程
dp[v] = max(dp[v], dp[v - w[i]] + c[i]);
}
}
//寻找dp[0..V]中最大的即为答案
int max = 0;
for(int v = 0; v <= V; v++){
if(dp[v] > max){
max = dp[v];
}
}
printf("%d\n", max);
return 0;
}
/*
输入:
5 8 //n==5,V==8
3 5 1 2 2 //w[i]
4 5 2 1 3 //c[i]
输出:
16
*/
完全背包问题
最新推荐文章于 2024-04-23 15:08:59 发布