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 <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
//#include <fstream>	// require freopen

using namespace std;

int main()
{
//	cut here before submit 
//	freopen ("testcase.splitup", "r", stdin );
	int n;

	while (cin >> n && n ){
		vector <int> party(n);
		for (int i = 0; i < n; ++i ){
			cin >> party[i];
		} // end for

		int termA = 0, termB = 0; 
		int res = INT_MAX;

		termA = party[0];

		if (n == 1 ){
			return termA;
		}else if (n == 2 ){
			termB = party[1];
			res = abs (termA - termB );
			cout << res << endl;
		}else{ // end if

			for (int i = 0; i < (1 << n); ++i ){
				termA = 0, termB = 0;
				for (int j = 0; j < n; ++j ){
					if (i & (1 << j ) ){
						termA += party[j];
					}else{
						termB += party[j];
					} // end if
				} // end for
				int diff = abs (termA - termB );
				res = min (res, diff );
			} // end for
			cout << res << endl;
		} // end if
	} // end loop
		
	return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int dfs(int n, vector<int> &v, int A, int B){
  if(n >= v.size()) return abs(A - B);
  int ret = dfs(n + 1, v, A + v[n], B);
  ret = min(ret, dfs(n + 1, v, A, B + v[n]));
  return ret;
}

int main() {
  int N;
  while(cin >>N && N){
    vector<int> v(N); REP(i, N) cin >>v[i];
    cout <<dfs(0, v, 0, 0) <<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int n, a[23]; vector<int>v;
void f(int k, int sum) {
	if (k == n)v.push_back(sum);
	else {
		f(k + 1, sum);
		f(k + 1, sum + a[k]);
	}
	return;
}
int main() {
	while (cin >> n, n) {
		v.clear();
		int sum = 0;
		for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; }
		f(0, 0);
		int ans = sum;
		for (size_t i = 0; i < v.size(); i++) {
			int v1 = v[i], v2 = sum - v[i];
			ans = min(ans, abs(v1 - v2));
		}
		cout << ans << endl;
	}
}

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

using namespace std;

int N, a[20];

int rec(int n, int b, int c)
{
  if (n < 0) {
    return abs(b - c);
  }

  return min( rec(n - 1, b + a[n], c),
              rec(n - 1, b, c + a[n]));
}

int main()
{
  while (cin >> N && N) {
    
    // read
    for (int i = 0; i < N; i++) {
      cin >> a[i];
    }

    cout << rec(N - 1, 0, 0) << endl;
  }
}

C++: #include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;

int main(){
	
	int n;
	
	while(cin>>n,n!=0){
		int a=0,b=0;
		vector<int> s(n);
	
	for(int i=0;i<n;i++)
		cin>>s[i];
	
	sort(s.begin(),s.end(),greater<int>());
	
	
		for(int i=0;i<n;i++){
			if(s[i]>0){
			if(a>b)
				b+=s[i];
			else
				a+=s[i];
		}
			else{
				if(a>b)
				a+=s[i];
			else
				b+=s[i];
			}
		cout<<abs(a-b)<<endl;
	}
}

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

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;
		}
	}
}