http://acm.hdu.edu.cn/showproblem.php?pid=1171
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 int a[150005]; 6 struct mes{ 7 int v,num; 8 }op[55]; 9 void zeroonepack(int V,int v,int w) 10 { 11 for(int i=V;i>=v;i--) 12 a[i]=max(a[i],a[i-v]+w); 13 } 14 int main() 15 { 16 int n; 17 while(scanf("%d",&n)&&n>0){ 18 int x,y,j=1,V=0; 19 memset(a,0,sizeof(a)); 20 for(int i=0;i<n;i++){ 21 scanf("%d%d",&op[i].v,&op[i].num); 22 V+=op[i].v*op[i].num; 23 } 24 for(int i=0;i<n;i++){ 25 int j=1; 26 while(j<=op[i].num){ 27 zeroonepack(V/2,op[i].v*j,op[i].v*j); 28 op[i].num-=j; 29 j*=2; 30 } 31 if(op[i].num>0) 32 zeroonepack(V/2,op[i].num*op[i].v,op[i].num*op[i].v); 33 } 34 printf("%d %d\n",V-a[V/2],a[V/2]); 35 } 36 return 0; 37 }