题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171
#include <stdio.h>
#include <string.h>
#define MAXN 250000
int dp[MAXN],V;
void ZeroOnePack(int val,int w)
{
int i;
for(i=V;i>=val;--i)
if(dp[i]<dp[i-val]+w)
dp[i]=dp[i-val]+w;
}
void CompletePack(int val,int w)
{
int i;
for(i=val;i<=V;++i)
if(dp[i]<dp[i-val]+w)
dp[i]=dp[i-val]+w;
}
void MultPack(int val,int w,int n)
{
int k;
if(val*n>=V)
CompletePack(val,w);
else
{
k=1;
while(k<n)
{
ZeroOnePack(k*val,k*w);
n-=k;
k<<=1;
}
ZeroOnePack(n*val,n*w);
}
}
int main()
{
int i,n,Value[52],num[52],sum;
while(scanf("%d",&n)&&(n>0))
{
sum=0;
memset(dp,0,sizeof(dp));
for(i=1;i<=n;++i)
{
scanf("%d %d",&Value[i],&num[i]);
sum+=Value[i]*num[i];
}
V=sum/2;
for(i=1;i<=n;++i)
{
MultPack(Value[i],Value[i],num[i]);
}
printf("%d %d\n",sum-dp[V],dp[V]);
}
return 0;
}