HDU 1171 Big Event in HDU
0-1背包
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=55; int V[maxn],C[maxn]; int dp[maxn*maxn*110]; int main() { int n,i,j,k,summ; while(~scanf("%d",&n)) { if(n<=0) break; memset(dp,0,sizeof(dp)); dp[0]=1;summ=0; for(i=1; i<=n; i++) { scanf("%d%d",&V[i],&C[i]); summ=summ+V[i]*C[i]; } for(i=1; i<=n; i++) for(j=1; j<=C[i]; j++) for(k=summ; k>=0; k--) if(dp[k]==1) dp[k+V[i]]=1; for(i=summ/2; i>=0; i--) if(dp[i]) break; printf("%d %d\n",max(i,summ-i),min(i,summ-i)); } return 0; }