poj Cash Machine

时间:2024-10-12 09:37:37

http://poj.org/problem?id=1276

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define For(n) for(int i=1; i<=(n); i++)
#define maxn 100010
using namespace std; int dp[maxn];
struct node
{
int n;
int m;
}p[maxn];
int cash,n; int main()
{
while(scanf("%d%d",&cash,&n)!=EOF)
{
For(n) scanf("%d%d",&p[i].n,&p[i].m);
if(n==||cash==) {printf("0\n");continue;}
int max1=;
memset(dp,,sizeof(dp));
dp[]=;
for(int i=; i<=n; i++)
{
for(int j=max1; j>=; j--)
{
if(dp[j])
{
for(int k=; k<=p[i].n; k++)
{
int sum=j+k*p[i].m;
if(sum>cash) break;
dp[sum]=;
max1=max(max1,sum);
}
}
}
}
printf("%d\n",max1);
}
return ;
}