HUD 2159 完全背包
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define inf 99999999 #define maxn 105 using namespace std; int w[maxn],v[maxn],f[maxn][maxn]; int main() { int i,j,t,n,m,k,s,ans; while (~scanf("%d%d%d%d",&n,&m,&k,&s)) { for (i=1;i<=k;i++) scanf("%d%d",&v[i],&w[i]); memset(f,0,sizeof(f)); for (t=1;t<=k;t++) for (i=w[t];i<=m;i++) for (j=1;j<=s;j++) { f[i][j]=max(f[i][j],f[i-w[t]][j-1]+v[t]); } for (i=0;i<=m;i++) if (f[i][s]>=n) break; printf("%d ",m-i); } return 0; }