链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=456

 

价值与重量相等的01背包..背包的容量为邮票总和的一半

 

#include <iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[500005];
int v[1005];
int main()          //价值与总量相等的01背包
{
    //freopen("1.txt","r",stdin);
    int m,n;
    int i,j;
    int sum;
    int tv;
    cin>>m;
    while(m--)
    {

        cin>>n;
        sum=0;
        for(i=0;i<n;i++)
            {
                cin>>v[i];
                sum+=v[i];
            }
        memset(dp,0,sizeof(dp));
        tv=sum/2;
        for(i=0;i<n;i++)
            for(j=tv;j>=v[i];j--)
                dp[j]= max( dp[j], dp[j-v[i]]+v[i]);

        cout<<int(sum-dp[tv]*2)<<endl;
    }
    return 0;
}