11276 - Subset Sum   

Description

Given N integers, seperate all of them into two multisets A and B such that the absolute difference between their sums of elements, i.e. , is minimized.

Notice that:

1. Multiset is a generalization concept of a set that, unlike a set, allows multiple instances of the same element.

2. Either A or B is allowed to be empty, and the sum of elements of an empty multiset is defined as 0.

Input

Input contains multiple test cases.

Each test case starts with a positive integer N, then the next line contains N integers n1, n2, …nseperated by single space character. A test case of N = 0 indicates the end of input, and it should not be processed.

 

For dataset 1, 1N3, 0ni100
For dataset 2, 1
N5, 0ni10000
For dataset 3, 1
N10, 0ni100000
For dataset 4, 1
N20, -1000000ni1000000

Hint: Try to enumerate all possible combinitions.

Output

For each testcase, output an integer denoting the minimum value of   in a line.

Sample Input  Download

Sample Output  Download

Tags




Discuss