use fmax().-------TLE
#define mmax(a,b) (a)>(b)?(a):(b); ---796ms
that's it!
#include <stdio.h> #include <string.h> #include <math.h> #define mmax(a,b) (a)>(b)?(a):(b); int main(){ int dp[50*50*100+100]; int n,t,i,j,res; int w[250000+10]; int t1,t2; int cnt,sum; while(~scanf("%d",&n)){ if(n<=0) break; sum=0; cnt=0; memset(dp,0,sizeof(dp)); for(i=1;i<=n;++i){ scanf("%d%d",&t1,&t2); sum+=t1*t2; for(j=1;j<=t2;++j){ w[++cnt]=t1; } } int haha; haha=sum/2; for(i=1;i<=cnt;++i) for(j=haha;j>=w[i];--j) dp[j]=mmax(dp[j],dp[j-w[i]]+w[i]); t1=0; t1=dp[haha]; t2=sum-t1; if(t1<t2){ t=t1;t1=t2;t2=t; } printf("%d %d ",t1,t2); } return 0; }