#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int T;
while (cin >> T)
{
while (T--)
{
int n; cin >> n;
vector<int>coin(n);
int sum = 0;
for (int i = 0; i < n; i++)
{
cin >> coin[i];
sum += coin[i];
}
vector<bool>available(sum+1);
available[0] = available[sum] = true;
for (int i = 0; i < n; i++)
{
for (int j = sum; j >= coin[i]; j--)
{
if (!available[j])
{
available[j] = available[j - coin[i]];
}
}
}
for (int i = sum / 2; 1; i--)
{
if (available[i])
{
cout << sum - 2 * i << endl;
break;
}
}
}
}
return 0;
}
UVA_562_Dividing coins
最新推荐文章于 2019-11-11 19:48:29 发布