Create 1276_libin56842.cpp

This commit is contained in:
Kirigaya Kazuto 2016-09-01 20:32:53 +08:00 committed by GitHub
parent f756ccd9db
commit 74f43c6ec7

55
POJ/1276_libin56842.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
int n,v;
} a[20];
int dp[100010];
int main()
{
int sum,n,i,j,k;
while(~scanf("%d%d",&sum,&n))
{
for(i = 1; i<=n; i++)
scanf("%d%d",&a[i].n,&a[i].v);
if(!sum)
{
printf("0\n");
continue;
}
if(!n)
{
printf("0\n");
continue;
}
memset(dp,0,sizeof(dp));
dp[0] = 1;
int MAX = 0,tem;
for(i = 1; i<=n; i++)
{
for(j = MAX;j>=0;j--)
{
if(dp[j])
{
for(k = 1;k<=a[i].n;k++)
{
tem = j+k*a[i].v;
if(tem>sum)
continue;
dp[tem] = 1;
if(tem>MAX)
MAX = tem;
}
}
}
}
printf("%d\n",MAX);
}
return 0;
}