#include <vector>
#include <string>
using namespace std;
int SubSetSum(vector<int>num);
int n;
int main()
{
int val,i,test,res;
vector<int>num;
scanf("%d",&test);
while(test--)
{
scanf("%d",&n);
num.clear();
for(i=0;i<n;i++)
{
scanf("%d",&val);
num.push_back(val);
}
res = SubSetSum(num);
printf("%d\n",res);
}
return 0;
}
int SubSetSum(vector<int>num)
{
int i,j,high,l,mini,x,val;
vector<int>dp;
dp.push_back(0);
for(i=0;i<num.size();i++)
{
high=dp.size();
for(j=0;j<high;j++)
dp.push_back(num[i]+dp[j]);
/*for(j=high-1;j>=0;j--)//For Remove Repeatation
dp.push_back(num[i]+dp[j]);*///give same result
}
l = dp.size();
val = dp[l-1] / n;
x = dp[l-1] % n;
mini = x;
for(i=l-2;i>=0;i--)
{
x = dp[i] % n;
if(dp[i] / n == val)
{
mini = min(mini,x);
}
else break;
}
return mini;
}
No comments:
Post a Comment