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 <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <algorithm>
#include <cassert>

using namespace std;



bool solve(){
	while(1){
		int n;
		cin>> n;
		if(!n) break;
		
		vector<int> a(n);
		int sum=0;
		for(int i=0;i<n;i++){
			cin>> a[i];
			sum += a[i];
		}

		sort(a.begin(), a.end());
//		int lsum,hsum;
//		lsum = hsum = 0;
		int sum2 = sum;
		vector<vector<int> > hoge(n+1, vector<int>(sum+1, 0));
		hoge[0][0] = 1;
		for(int i=0;i<n;i++){
			for(int j=0;j<=sum;j++){
				hoge[i+1][j] = hoge[i][j];
				if(j>=a[i] && hoge[i][j-a[i]]){
					hoge[i+1][j] = 1;
					if(abs(j-sum/2) < abs(sum2-sum/2)) sum2 = j;
					if(sum%2 &&  abs(j-sum/2+1) < abs(sum2-sum/2+1)) sum2 = j;
				}
//				cout<< hoge[i+1][j];
			}
//			cout<< "   "<< sum2;
//			cout<< endl;
			if(sum2 == sum/2) break;
		}
		int ans = abs(sum2 - (sum-sum2));
		cout<< ans<< endl;
	}
	return true;
}



int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	solve();

	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;

int a[21];
int n;

int solve(int p, int s, int t)
{
  if(p == n) return abs(s-t);
  return min(solve(p+1, s+a[p], t), solve(p+1, s, t+a[p]));
}

int main()
{
  while(scanf("%d", &n)!=EOF && n != 0){
    for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
    printf("%d\n", solve(0, 0, 0));
  }

  return 0;
}

C++: #include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int n, sum; vector<int> a;
    
    while(true)
    {
        cin >> n;
        
        if(n == 0) { break; }
        
        a = vector<int>(n);
        
        for(int i = 0; i < n; i++)
        {
            cin >> a[i]; sum += a[i];
        }
        
        int min_ = (1 << 29);
        
        for(int i = 0; i <(1 << n); i++)
        {
            int sum2 = 0;
            
            for(int j = 0; j < n; j++)
            {
                if(i & (1 << j))
                {
                    sum2 += a[j];
                }
            }
            
            min_ = min(abs(2 * sum2 - sum), min_);
        }
        
        cout << min_ << endl;
    }
    
    return 0;
}

C++: //#define LOCAL

#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm>

#define rep(n) for(int i=0; i<n; i++)

using namespace std;

int n;
int a[30];
int ans;

void rec(int i, int A, int B)
{
    if (i == n) {
        ans = min(ans, abs(A - B));
        return;
    }

    rec(i + 1, A + a[i], B);
    rec(i + 1, A, B + a[i]);
}

int solve()
{
    ans = 30000000;
    rec(0, 0, 0);
    return ans;
}

int main()
{
#ifdef LOCAL
    ifstream in("input.txt");
    cin.rdbuf(in.rdbuf());
#endif

    while (true) {
        cin >> n;
        if (n == 0) break;

        rep(n) {
            cin >> a[i];
        }

        cout << solve() << endl;
    }
}

C++: #include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

int main( void )
{
	int n;
	int teamA, teamB, map[20];
	while(cin >> n, n) {
		for (int i = 0; i < n; ++i) cin >> map[i];
		sort(map, map+n, greater<int>());
		teamA = teamB = 0;
		for (int i = 0; i < n; ++i) {
			if(teamA > teamB)	teamB += map[i];
			else				teamA += map[i];
		}
		cout << abs(teamA - teamB) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		vector<long long int> d(n);
		long long int a = 0;
		for (int i= 0; i < n; i++) {
			cin >> d[i];
			a += d[i];
		}
		int l = 1<<n;
		long long int MIN = a;
		for (int i = 0; i < l; i++) {
			long long int b = 0;
			for (int j = 0; j < n; j++) {
				if ((i & (1<<j)) != 0) {
					b += d[j];
				}
			}
			MIN = min(MIN, abs(abs(a-b)-b));
		}
		cout << MIN << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

int n, m[20];
vector<int>X1;
vector<int>X2;
int Mid = 0;

int main() {
	while (true) {
		X1.clear();
		X2.clear();
		int sum = 0;
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> m[i];
			sum += m[i];
		}
		Mid = n / 2;
		int s = 0;
		for (int i = 0; i < (1 << Mid); i++) {
			s = 0;
			for (int j = 0; j < Mid; j++) {
				if ((i / (1 << j)) % 2 == 0) {
					s += m[j];
				}
			}
			X1.push_back(s);
		}
		for (int i = 0; i < (1 << (n - Mid)); i++) {
			s = 0;
			for (int j = 0; j < (n - Mid); j++) {
				if ((i / (1 << j)) % 2 == 0) {
					s += m[Mid + j];
				}
			}
			X2.push_back(s);
		}
		int minx = 1000000000;
		sort(X2.begin(), X2.end());
		for (int i = 0; i < X1.size(); i++) {
			if (X1[i] <= sum / 2) {
				int target = sum / 2 - X1[i];
				int p = upper_bound(X2.begin(), X2.end(), target) - X2.begin();
				p--;
				for (int j = 0; j < 3; j++) {
					if (p >= 0 && p < X2.size()) {
						int a = X2[p] + X1[i];
						int b = sum - a;
						minx = min(minx, abs(a - b));
					}
					p++;
				}
			}
		}
		cout << minx << endl;
	}
	return 0;
}