F. Fairness

time limit per test

2.0 s

memory limit per test

64 MB

input

standard input

output

standard output

Dwik and his brother Samir both received scholarships from a famous university in India. Their father, Besher, wants to send some money with each of them.

Besher has n coins, the ith coin has a value of ai. He will distribute these coins between his two sons in n steps. In the ith step, he chooses whether to give the ith coin to Dwik or to Samir.

Let xi be the absolute difference between the sum of Dwik's and Samir's coins after the ith step. The unfairness factor of a distribution is max({x1, x2, ..., xn}). Besher wants to minimize the unfairness factor, can you help him?

Input

The first line of the input consists of a single integer t, the number of test cases. Each test case consists of 2 lines:

The first line contains an integer n (1 ≤ n ≤ 100).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100).

Output

Print t lines, ith line containing a single integer, the answer to the ith test case.

Example
input

Copy
2
5
1 2 1 4 3
7
4 5 6 1 1 3 4
output

Copy
2
5
Note

In the first sample test, besher has 5 coins (1, 2, 1, 4, 3), he can distribute them in the following way:

Step 1: Give the first coin to dwik , d = 1, s = 0  x1 = |1 - 0| = 1

Step 2: Give the second coin to samir, d = 1, s = 2  x2 = |1 - 2| = 1

Step 3: Give the third coin to samir, d = 1, s = 3  x3 = |1 - 3| = 2

Step 4: Give the fourth coin to dwik, d = 5, s = 3  x4 = |5 - 3| = 2

Step 5: Give the fifth coin to samir, d = 5, s = 6  x5 = |5 - 6| = 1

max({x1, x2, x3, x4, x5}) = 2

题意:n个硬币分给a,b两个人,每分一个硬币对a,b当前硬币数量作差,分完之后,取分配过程中的最大差为最终权值。问在所有分配方法中,最终权值的最小值为多少

题解:数据量不大,暴力搜索每一种分配方法,求出每一种分配方法的最大差,取最小权值

#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<math.h>
#define mod 998244353
#define ll long long
#define MAX 0x3f3f3f3f
using namespace std;
int p[];
int n,t,mx;
void dfs(int a,int b,int k,int now)
{
if(k>=n)//目标状态,更新分硬币过程中的最大差值
{
mx=now;
return;
}
if(now>=mx)//如果比上一种分硬币方法的最大差值还大,就返回,换一种分法
{
return;
}
else
{
if(abs(a-b)>now)//更新当前差
now=abs(a-b);
dfs(a+p[k],b,k+,now);
dfs(a,b+p[k],k+,now);
} }
int main()
{
cin>>t;
while(t--)
{
mx=;
cin>>n;
for(int i=;i<n;i++)
cin>>p[i];
dfs(p[],,,);//第一个给谁差都一样
cout<<mx<<endl;
}
}

最新文章

  1. CenOS 7 安装wordpress
  2. 《zw版&#183;Halcon-delphi系列原创教程》 Halcon分类函数006, image,影像处理(像素图)
  3. Python入门-引号
  4. [示例]NSDictionary编程题-字典的排序应用(iOS6班)
  5. MySQL 按日期分表
  6. Delphi的&quot;Invalid pointer operation&quot;异常的解决办法
  7. CSS3 用户界面
  8. JavaScript中的面向对象的讨论(转)
  9. block, inline和inline-block的区别
  10. selenium2入门 用selenium安装、加载、启用插件(一)
  11. 理解交互设计之&quot;行为设计与对象设计&quot;
  12. vue2.0父子组件以及非父子组件如何通信
  13. [20160711][neven代码移植Windows]
  14. 多租户通用权限设计(基于casbin)
  15. Java_String
  16. 【备忘】mybatis的条件判断用&lt;choose&gt;
  17. vue中的单文件组件
  18. $fn、$extends $fn.extends的用法,jquery的插件开发
  19. iOS学习笔记(二)——Hello iOS
  20. dockerfile mysql

热门文章

  1. Go的WaitGroup
  2. 正确使用 Android 的 Theme 和 Style
  3. cookie、session、localStorage、sessionStorage的区别
  4. 移动端禁止缩放&lt;meta&gt;
  5. 通过Java读取xml文件内容
  6. springcloud-zuul进阶篇
  7. 表格中td限宽溢出以省略号代替
  8. luffy 那点事
  9. 【攻防世界】 高手进阶区 Recho WP
  10. 第1节 storm编程:3、storm的架构模型的介绍