简单dp
背包水题
#include <iostream>
#include <cstring>
using namespace std;
int dp[500001];
int v[101];
int ans[101];
int abs(int a)
{
if (a>=0)
return a;
else
return -a;
}
int main()
{
int r;
cin>>r;
for (int k=1;k<=r;k++)
{
int n;
cin>>n;
int sum=0;
for (int i=1;i<=n;i++)
{
cin>>v[i];
sum+=v[i];
}
memset (dp,0,sizeof(dp));
dp[0]=1;
for (int i=1;i<=n;i++)
{
for (int t=sum;t>=v[i];t--)
{
if (dp[t-v[i]])
dp[t]=1;
}
}
int min=99999999;
for (int i=0;i<=sum;i++)
{
if (dp[i]&&abs(sum-i*2)<min)
min=abs(sum-i*2);
}
cout<<min<<endl;
}
//system("pause");
return 0;
}