NYOJ_17_开心的小明
很简单的01背包:
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> using namespace std; int dp[30005]; int main() { int t,v,n,i,j,val,level; scanf("%d",&t); while(t--) { scanf("%d%d",&v,&n); memset(dp,0,sizeof(dp)); for(i=0;i<n;++i) { scanf("%d%d",&val,&level); for(j=v;j>=val;--j) { dp[j]=dp[j]<dp[j-val]+val*level?dp[j-val]+val*level:dp[j]; } } printf("%d\n",dp[v]); } return 0; }