735 3 4 125 6 5 3 350 633 4 500 30 6 100 1 5 0 1 735 0 0 3 10 100 10 50 10 10
735 630 0 0
#include <iostream>
#include <cstdio>
using namespace std;
int dist[105000];
int value[15];
int acount[15];
int n , m ;
void ZeroOnePack(int v)
{
for(int i = m; i>= v; i --)
{
dist[i] = max(dist[i] , dist[i-v]+v);
}
}
void CompletePack(int v)
{
for(int i = v; i <= m; i ++)
{
dist[i] = max(dist[i] , dist[i-v]+v);
}
}
void MultiPack(int v , int amount)
{
if(v * amount >= m)
{
CompletePack(v);
}
else
{
int k = 1;
int cnt = amount;
while(k <= cnt)
{
ZeroOnePack(k*v);
cnt -= k ;
k *= 2;
}
if(cnt)
ZeroOnePack(cnt*v);
}
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
if(n == 0){
printf("0\n");
continue;
}
for(int i = 0; i < n; i ++)
{
scanf("%d %d",&acount[i],&value[i]);
}
for(int i = 0; i <= m; i ++)
{
dist[i] = 0;
}
for(int i = 0; i < n; i ++)
{
MultiPack(value[i] , acount[i]);
}
printf("%d\n",dist[m]);
}
return 0;
}
原文:http://blog.csdn.net/wuhuajunbao/article/details/23671079