Test instructions: give you n different equipment, the value of each device is V, the number of each device is M, please try to divide them into two piles of similar total value, for the value of the two piles is how much, large in front of the output.
Analysis: See many versions on the web, can be used with the mother function, multi-backpack solution, here with 0-1 backpack solution.
Realization: Half of the value of gross position look for the backpack capacity, value for quality, value is also seen as volume; Finally, the value of nearly half of the total value can be found, the other half of the total value minus the half of the value of the backpack, notice that the total value of the backpack can be less than the total value less than half of the value of the
#include <iostream>using namespace Std;int value[5005];int dp[250005];int main () {int N,v,m;int sum,i,j,cnt;while (Cin>>n && n>0) {for (i=0,sum=0,cnt=0;i<n;i++) {cin>>v>>m;while (m--) {value[cnt++]=v; Sum+=v;}} Memset (Dp,0,sizeof (DP)); for (i=0;i<cnt;i++) for (j=sum/2;j>=value[i];j--) if (dp[j]<dp[j-value[i]]+value[i ]) dp[j]=dp[j-value[i]]+value[i];cout<<sum-dp[sum/2]<< "" <<dp[sum/2]<<endl;} return 0;}
HDU ACM 1171 Big Event in HDU