/*
* zero one knaspsack
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 50001
#define MAXCNT 101
int dp[MAXN], val[MAXCNT];
int zero_one_knaspsack(int n, int max_v)
{
memset(dp, 0, sizeof(dp)); dp[0] = 1;
for(int i = 0; i < n; i ++) {
for(int v = max_v/2; v >= val[i]; v --) {
if( dp[v-val[i]] ) {
dp[v] = 1;
}
}
}
for(int i = max_v/2; i >= 0; i --) {
if( dp[i] ) {
return ((max_v-i-i)>0)? (max_v-i-i) : -(max_v-i-i);
}
}
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int cas, n, max_v;
scanf("%d", &cas);
while( cas -- ) {
scanf("%d", &n); max_v = 0;
for(int i = 0; i < n; i ++) {
scanf("%d", &val[i]);
max_v += val[i];
}
printf("%d\n", zero_one_knaspsack(n, max_v));
}
return 0;
}
uva_562
最新推荐文章于 2019-04-29 13:34:38 发布