poj 1276 Cash Machine_多重背包

时间:2023-03-08 17:46:58

题意:略

多重背包

#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAXV 15
#define MAXM 100050 int cash,n,value[MAXV],c[MAXV],f[MAXM],user[MAXM]; int main(){
int i,j,max;
while(~scanf("%d%d",&cash,&n)){
for(i=1;i<=n;i++)
scanf("%d%d",&c[i],&value[i]); memset(f,0,sizeof(f));
f[0]=1;
max=0; for(i=1;i<=n;i++){
memset(user,0,sizeof(user));
for(j=value[i];j<=cash;j++)
if(!f[j] && f[j-value[i]] && user[j-value[i]]+1<=c[i]){
user[j]=user[j-value[i]]+1;
f[j]=1; //j为1,即j是可取的,选出可取的最大的j就是答案
if(j>max) max=j;
}
}
printf("%d\n",max); }
return 0;
}