Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	for {
		n := getNextInt(scanner)
		if n == 0 {
			break
		}
		sum := 0
		ll := make([]int, 1)
		rr := make([]int, 1)
		for i := 0; i < n/2; i++ {
			a := getNextInt(scanner)
			sum += a
			ll = merge(ll, a)
		}
		for i := n / 2; i < n; i++ {
			a := getNextInt(scanner)
			sum += a
			rr = merge(rr, a)
		}
		r := len(rr) - 1
		var ans cint
		ans = math.MaxInt32
		for l := 0; l < len(ll); l++ {
			for r >= 0 && ll[l]+rr[r] > 0 {
				r--
			}
			if r >= 0 {
				ans.minAs(cint(ll[l] + rr[r]).abs())
			}
			if r+1 < len(rr) {
				ans.minAs(cint(ll[l] + rr[r+1]).abs())
			}
		}
		fmt.Fprintln(writer, ans)
	}
}

type cint int64

func (ct *cint) minAs(x cint) *cint {
	*ct = ct.min(x)
	return ct
}
func (ct *cint) maxAs(x cint) *cint {
	*ct = ct.max(x)
	return ct
}
func (ct cint) min(x cint) cint {
	if ct > x {
		return x
	}
	return ct
}
func (ct cint) max(x cint) cint {
	if ct < x {
		return x
	}
	return ct
}
func (ct cint) abs() cint {
	return ct.max(-ct)
}
func merge(aa []int, a int) []int {
	n := len(aa)
	bb := make([]int, n)
	cc := make([]int, 0)
	for i := 0; i < n; i++ {
		bb[i] = aa[i] + a
		aa[i] = aa[i] - a
	}
	i := 0
	j := 0
	for i < n || j < n {
		if first(aa, bb, i, j, n) {
			cc = append(cc, aa[i])
			i++
			continue
		}
		cc = append(cc, bb[j])
		j++
	}
	return cc
}
func first(aa, bb []int, a, b, n int) bool {
	if a == n {
		return false
	}
	if b == n {
		return true
	}
	return aa[a] < bb[b]
}

C++: #include <bits/stdc++.h>

using namespace std;

template <typename T>
using vec1 = vector<T>;

template <typename T>
T cin2var()
{
    T val;
    cin >> val;
    return val;
}

template <typename T>
vec1<T> cin2vec(size_t size)
{
    vec1<T> vec1;
    for (size_t i = 0; i < size; ++i) {
        vec1.push_back(cin2var<T>());
    }
    return vec1;
}

template <typename T>
T ToGrayCode(T num)
{
    return num ^ (num >> 1);
}

using bs24 = bitset<24>;

int FindBit(const bs24& bs)
{
    for (size_t i = 0; i < 24; ++i) {
        if (bs.test(i)) {
            return i;
        }
    }
    return -1;
}

int main()
{
    while (true) {
        const auto N(cin2var<size_t>());
        if (!N) break;
        const auto   Ns(cin2vec<int>(N));
        const size_t count     = 1 << N;
        int          sum_a     = 0;
        int          sum_b     = accumulate(Ns.begin(), Ns.end(), 0);
        int          diff_min  = sum_b;
        size_t       prev_gray = 0;
        for (size_t i = 1; i < count; ++i) {
            const size_t gray = ToGrayCode(i);
            const size_t diff = gray ^ prev_gray;
            const bs24 bs_diff(diff);
            const int  diff_pos = FindBit(bs_diff);
            const auto pow = Ns[diff_pos];
            const bs24 bs_gray(gray);
            if (bs_gray.test(diff_pos)) {
                // move A <- B
                sum_a += pow;
                sum_b -= pow;
            }
            else {
                // move A -> B
                sum_a -= pow;
                sum_b += pow;
            }
            const auto diff_ab = abs(sum_a - sum_b);
            if (diff_min > diff_ab) {
                diff_min = diff_ab;
            }
            prev_gray = gray;
        }
        cout << diff_min << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cmath>
using namespace std;
#define rep(i, n) for(int (i) = 0; (i) < (n); i++)

int n; //登録者数
int w[30]; //各登録者の戦闘力

int dfs(int A, int B, int m){//AはグループAの総力、BはグループBの総力、mは登録者の順番
    int a, b;
    if(m >= n) return abs(A - B); 
    a = dfs(A + w[m], B, m + 1);
    b = dfs(A, B + w[m], m + 1); 
    return min(a, b);
}

int main(void){
    while(true){
        cin >> n;
        if(n == 0) break;
        rep(i, n) cin >> w[i];

        cout << dfs(0, 0, 0) << endl;
    }
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;

int n;
int mn;
int a[21];

void dfs(int battle , int depth){
	if(depth==n){
		mn=min( mn , abs(battle));
	} else {
		//cout << "left >battle:" << abs(battle+a[depth]) << " depth:" << depth << endl;
		dfs(battle+a[depth] , depth+1);
		//cout << "right>battle:" << abs(battle-a[depth]) << " depth:" << depth << endl;
		dfs(battle-a[depth] , depth+1);
	}
}


int main(){
	while(cin>>n , n!=0){
		for(int i=0;i<n;++i){
			cin >> a[i];
		}
		mn=1000001;
		dfs(0 , 0);
		cout << mn << endl;
	}
	return 0; 
}