DWITE Online Computer Programming Contest

Balancing Act

February 2011
Problem 3

Being the star attraction of the local carnival, it’s your job to come up with new acts for every show. For the next show, you decided that you would give a shot at riding a unicycle while balancing weights on both of your hands. To make sure that you don’t fall, you want the weights in your hands to balance out as much as possible (i.e. the difference between the sum of the weights in either of your hands must be minimized). Given the weights you have, determine the smallest difference in weight between your left and right hands.

The input file DATA3.txt will contain 5 test cases. In each case, the first line contains a number N (1 ≤ N ≤ 30) representing the number of weights you have. The next N lines each contain a number W (1 ≤ W ≤ 1000) representing how heavy the weight is.

The output file OUT3.txt should contain 5 lines, with each line representing the minimum absolute difference of weight sums between your two hands.

Note: You must use all the given weights. The trivial solution of having zero weight in both hands is not acceptable. 😉

Sample Input (first two cases shown):
3
1
10
6
5
9
4
5
7
1
Sample Output (first two cases shown):
3
0