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<vector>
#include<algorithm>

using namespace std;

int main()
{
	int n;
	
	for(;;)
	{
		vector<int> num;
		cin >> n;
		if(n==0)
			break;
		for(int i=0;i<n;i++)
		{
			int a;
			cin >> a;
			num.push_back(a);
		}
		
		sort(num.begin(),num.end());
		
		int a=0,b=0;
		int a_f=1;
		
		if(num.size()==2)
		{
			cout << sqrt((num[0]-num[1])*(num[0]-num[1])) << endl;
		}
		else
		{
			if(num.size()%2==0)
			{
				for(int i=0,j=num.size()-1;i<j;i++,j--)
				{
					if(a_f==1)
					{
						a+=num[i]+num[j];
						a_f=0;
					}
					else
					{
						b+=num[i]+num[j];
						a_f=1;
					}
				}
			}
			else
			{
				for(int i=1,j=num.size()-1;i<=j || i<num.size();i++,j--)
				{
					if(a_f==1)
					{
						a+=num[i]+num[j];
						a_f=0;
					}
					else
					{
						b+=num[i]+num[j];
						a_f=1;
					}
				}
				if(a<=b)
					a+=num[0];
				else
					b+=num[0];
			}
			cout << "a:" << a << " b:" << b << endl;
			cout << sqrt((a-b)*(a-b)) << endl;
		}
	}
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <complex>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int dp[2][21];
int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		int sum = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
			dp[0][i] = dp[1][i] = 1000000000;
		}
		int k = 0;
		rep(i, n) {
			dp[k][i] = sum - 2*a[i];
			rep(j, i) {
				if (dp[1-k][j] > 0) {
					if (abs(dp[k][j]) > abs(dp[1-k][j]-2*a[i]))
						dp[k][j] = dp[1-k][j] - 2*a[i];
				}
				/*
				else if (dp[1-k][j] < 0) {
					if (abs(dp[k][j]) > abs(dp[1-k][j]+2*a[i]))
						dp[k][j] = dp[1-k][j] + 2*a[i];
				}
				*/
				if (abs(dp[k][j]) > abs(dp[1-k][j])) {
					dp[k][j] = dp[1-k][j];
				}
			}
			k = 1 - k;
		}
		int res = 1000000000;
		rep(i, n) res = min(res, abs(dp[1-k][i]));
		cout << res << endl;
	}
	return 0;
}

#if 0
int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		int sum = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
		}
		int res = 1000000000;
		rep(i, (1<<n)) {
			int A = 0;
			rep(j, n) if (i & (1 << j)) A += a[j];
			res = min(res, abs(A - (sum - A)));
		}
		cout << res << endl;
	}
	return 0;
}
#endif

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

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end())); }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }
TEMP_T T    cums(T& v){ T r; r.push_back(v[0]); for(int i=1; i<v.size(); i++) r.push_back(r[i-1] + v[i]); return r; }
TEMP_T void maxe(T& v, T m){ v = max(v, m); }
TEMP_T void mine(T& v, T m){ v = min(v, m); }

static inline int in(){ int x; scanf("%d", &x); return x; }

struct Solver {
  int mini;
  vector<int> vs;
  Solver(vector<int> vs) : vs(vs), mini(INT_MAX) {}
  void solve(int it=0, int a=0, int b=0){
    if (it == vs.size()){
      mine(mini, abs(a - b));
      return;
    }
    solve(it + 1, a + vs[it], b);
    solve(it + 1, a, b + vs[it]);
  }
  int get_min(){ return mini; }
};
int main()
{
  int n;
  while (n = in()){
    vector<int> vs;
    for (int i = 0; i < n; i++){
      vs.push_back(in());
    }
    Solver solver(vs);
    solver.solve();
    printf("%d\n", solver.get_min());
  }

  return 0;
}

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

using namespace std;

vector<int> ans;
int main(){
	while(1){
		int n;
		cin >> n;
		
		if(n == 0) break;
		
		int a[20];
		for(int i = 0;i < n;i++){
			cin >> a[i];
		}
		
		int tans = 1e9;
		for(int i = 0;i < (1 << n);i++){
			int A=0,B=0;
			for(int j = n-1;j >= 0;j--){
				if((i >> j) % 2 == 0){
					A += a[j];
				}else{
					B += a[j];
				}
			}
			tans = min(tans,abs(A - B));
		}
		
		ans.push_back(tans);
	}
	
	for(int i = 0;i < ans.size();i++) cout << ans[i] << endl;
	return 0;
}