Accepted
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<vector>
#include<cmath>
#include<cctype>
#include<sstream>
#include<set>
#include<list>
#include<stack>
#include<utility>
#include<queue>
#include<algorithm>
#include<cstdlib>
// End
//..........
// Macro
#define sf scanf
#define pf printf
#define lp1(i,n) for(i=0;i<n;i++)
#define lp2(i,n) for(i=1;i<=n;i++)
#define mem(c,v) memset(c,v,sizeof(c))
#define cp(a) cout<<" "<<a<<" "
#define nl puts("")
#define sq(x) ((x)*(x))
#define all(x) x.begin(),x.end()
#define reall(x) x.rbegin(),x.rend()
#define sz size()
#define gc getchar()
#define pb push_back
#define freader freopen("input.txt","r",stdin)
// End.........
// Size
#define mx7 20000100
#define mx6 1500000
#define mx5 100005
#define mx4 1000100
#define inf 1<<30 //infinity value
#define eps 1e-9
#define mx 1009
#define mod 1000000007
#define pi acos(-1.0)
// Macros for Graph
#define white 1
#define gray 2
#define black 3
#define nil 0
using namespace std;
int v[1001][50005],arr[mx+9];
int knapsack(int n,int m_x)
{
for(int i=0;i<=m_x;i++)
{
v[0][i]=0;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m_x;j++)
{
if(arr[i]<=j)
{
v[i][j] = max(v[i-1][j] , v[i-1][j-arr[i]] + arr[i]);
}
else
{
v[i][j] = v[i-1][j];
}
}
}
return v[n][m_x];
}
int main()
{
int t;
sf("%d",&t);
while(t--)
{
mem(arr,0);
int no_coin,sum=0;
sf("%d",&no_coin);
for(int i=1;i<=no_coin;i++)
{
sf("%d",&arr[i]);
sum+=arr[i];
}
int avg = sum/2;
int r=knapsack(no_coin,avg); //cout << sum << " " <<r; nl;
pf("%d\n",(sum - (r*2)));
}
return 0;
}
Comments
Post a Comment