UVA 562 Dividing coins (01背包)

时间:2024-06-05 22:35:08

//平分硬币问题

//对sum/2进行01背包,sum-2*dp[sum/2]

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; int value[],dp[]; int main()
{
int n,m,sum,sum1;
cin>>n;
while(n--)
{
cin>>m;
sum=;
for(int i=;i<=m;i++)
{
cin>>value[i];
sum+=value[i];
}
sum1=sum/;
memset(dp,,sizeof(dp));
for(int i=;i<=m;i++)
for(int j=sum1;j>=value[i];j--)
dp[j]=max(dp[j],dp[j-value[i]]+value[i]);
cout<<sum-*dp[sum1]<<endl;
}
return ;
}