UVa 562 Dividing coins (0-1 backpack & equivalent conversion)

Source: Internet
Author: User

562-dividing Coins

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=503

It ' s commonly known that Dutch have invented Copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and fighting is so fierce, They stretched the coin to great length and thus created Copper-wire. Not commonly known was that the fighting started, after the two Dutch-tried to divide a bag with coins between the two of T Hem. The contents of the bag appeared is equally divisible. The Dutch of the past couldn ' t stand the fact that a division should favour one of them and they always a wanted fair E to the very last cent. Nowadays fighting over a single cent won't be seen anymore, but being capable of making a equal division as fair as PO Ssible is something that would remain important forever ...

That ' s what this whole problem is about. Not everyone is capable of seeing instantly what ' s most fair division of a bag of coins between-two. Your help are asked to solve this problem.

Given a bag with a maximum of coins, determine the most fair, division and between two persons. This means difference between the amount of each person obtains should to be minimised. The value of a coin varies from 1 cent to cents. It ' s not allowed to split a single coin.

Input

Line A and the number of problems N, followed by N times:

A line with a non negative an integer m() indicating the number of coins in the bag

Line A and M numbers separated by one spaces, each number indicates the value of a coin.

Output

The output consists of n lines. Each line contains the minimal positive difference between the amount the two persons-obtain when they divide the coins fr Om the corresponding bag.

Sample Input

2
3
2 3 5
4
1 2 4 6

Sample Output

0
1

Thinking: Equivalence transformation. Equal amount (capacity) for sum the package is equivalent to using those coins to install a SUM/2 bag (the remaining coin is another package), this is not 0-1 backpack it ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.