#include<stdio.h> #include<stdlib.h> #include<string.h> int max(int a,int b) { if(a>b) return a; else return b; } int main(void) { int n,c,i,j; scanf("%d %d",&n,&c); int rl[n+1][c+1]; int w[n+1],v[n+1]; memset(rl,0,sizeof(rl)); memset(w,0,sizeof(w)); memset(v,0,sizeof(v)); for(i = 1;i<=n;i++) { scanf("%d",&w[i]); scanf("%d",&v[i]); } for(i = 1;i <= n;i++) { for(j = 1;j<=c;j++) { if(j<w[i]) rl[i][j] = rl[i-1][j]; else rl[i][j] = max(rl[i-1][j],rl[i-1][j-w[i]]+v[i]); } } printf("%d",rl[n][c]); return 0; }
原文:https://www.cnblogs.com/loliconsk/p/14137982.html