传送门:https://www.luogu.org/problemnew/show/P1060
#include<cstdio> #include<algorithm> using namespace std; int n,m,w[30],v[30],f[30001]; int main() { scanf("%d%d",&n,&m); for(int i = 1;i <= m;i++) { scanf("%d%d",&w[i],&v[i]); v[i]*=w[i]; } for(int i = 1;i <= m;i++) for(int j = n;j >= w[i];j--) f[j] = max(f[j],f[j-w[i]]+v[i]); printf("%d",f[n]); return 0; }