//将背包的体积设置为总容量的一半
//使用多重背包看一半体积最多能放下多少东西
#include<iostream>
using namespace std;
int V,i,j;
int f[250000];
int value[55];
int number[55];
int max(int a,int b){return a>b?a:b;}
void zeroOnePack(int cost,int weight){
for(int v=V;v>=cost;v--)
f[v]=max(f[v],f[v-cost]+weight);
}
void completePack(int cost,int weight){
for(int v=cost;v<=V;v++)
f[v]=max(f[v],f[v-cost]+weight);
}
void multiplePack(int cost,int weight,int amount){
if(cost*amount>=V){
completePack(cost,weight);
return;
}
int k=1;
while(k<amount){
zeroOnePack(k*cost,k*weight);
amount=amount-k;
k=k*2;
}
zeroOnePack(amount*cost,amount*weight);
}
int main(){
int n;
while(cin>>n){
if(n<0)
break;
memset(f,0,sizeof(f));
int sum=0;
for(i=1;i<=n;i++){
cin>>value[i]>>number[i];
sum+=value[i]*number[i];
}
V=sum>>1;
for(i=1;i<=n;i++){
multiplePack(value[i],value[i],number[i]);
}
if(f[V]<sum-f[V])
cout<<sum-f[V]<<" "<<f[V]<<endl;
else
cout<<f[V]<<" "<<sum-f[V]<<endl;
}
return 0;
}
hdu 1171
最新推荐文章于 2021-02-21 23:08:52 发布