# hdu1171、hdu2602(简单01背包)

hdu1171

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int v[5000];
int s[250000];
int main(void)
{
int n;
while(cin>>n&&n>0)
{
memset(v,0,sizeof(v));
memset(s,0,sizeof(s));
int total=0;
int value,amount;
int i,j;
int k=1;
for(i=1;i<=n;i++)
{
cin>>value>>amount;
total+=value*amount;
while(amount)
{
v[k]=value;
k++;
amount--;
}
}
int max_=-1;
for(i=1;i<k;i++)
for(j=total/2;j>=v[i];j--)
{
s[j]=max(s[j],s[j-v[i]]+v[i]);
}
for(j=total/2;j>=v[i];j--)
if(s[j]>max_)
max_=s[j];
cout<<total-max_<<" "<<max_<<endl;
}
return 0;
}


hdu2602

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int N[1000],V[1000];
int maxsum[1000000];
int main(void)
{
int m;
cin>>m;
while(m)
{
memset(maxsum,0,sizeof(maxsum));
int n,v;
int i,j;
cin>>n>>v;
for(i=1;i<=n;i++)
cin>>V[i];
for(i=1;i<=n;i++)
cin>>N[i];
for(i=1;i<=n;i++)
for(j=v;j>=N[i];j--)
maxsum[j]=max(maxsum[j],maxsum[j-N[i]]+V[i]);
cout<<maxsum[v]<<endl;
m--;
}
return 0;
}