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 <cstring>

using namespace std;

bool table[1000001];

int main(){
	while(true){
		int n, a, sum = 0;
		cin >> n;
		if(n == 0){ break; }
		memset(table, 0, sizeof(bool) * 1000001);
		table[0] = true;
		while(n--){
			cin >> a;
			for(int i = sum; i >= 0; --i){
				if(table[i]){ table[i + a] = true; }
			}
			sum += a;
		}
		int answer = sum / 2;
		for(; answer >= 0; --answer){
			if(table[answer]){ break; }
		}
		cout << (sum - answer * 2) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int n,u[21];
vector <int> a,b;
int minsa = 100000;

void kakunin() {
  cout << "ateam" << endl;
  for(int i=0;i<a.size();i++) cout << a[i] <<" ";
  cout << endl<< "bteam" << endl;
  for(int i=0;i<b.size();i++) cout << b[i] << " " ;
  cout << endl<<"----------------------"<<endl;;
}

void sa() {
  //  kakunin();
  int sum = 0;  
  for(int i=0;i<a.size();i++) sum += a[i];
  for(int i=0;i<b.size();i++) sum -= b[i];
  if(sum < 0) sum *= -1;
  if(minsa > sum) minsa = sum;
}
				
				
void check(int x) {
  //  kakunin();
  int flag = 0;
  if(x == n) {
    sa();
    flag = 1;
  }
  else if(flag == 0) {
    a.push_back(u[x]);
    check(x+1);
    a.pop_back();
    
    b.push_back(u[x]);
    check(x+1);
    b.pop_back();
  }
} 

int main() {

  while(1) {
    cin >> n;
    if(n == 0) break;
    for(int i=0;i<n;i++) cin >> u[i];
    check(0);
    a.clear();
    b.clear();
    cout << minsa << endl;
  }

  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int a[30];

int main() {
	int n;
	while(n=in()){
		int i,sum=0,A;
		for(i=0;i<n;i++){
			cin>>a[i];
			sum+=a[i];
		}
		int res=100100100;
		for(i=0;i<1<<n;i++){
			A=0;
			int j;
			for(j=0;j<n;j++){
				if(i>>j&1){
					A+=a[j];
				}
			}
//cout<<i<<" "<<A<<endl;
			chmin(res,abs(sum-2*A));
		}
		cout<<res<<endl;
	}
	return 0;
}

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

using namespace std;

int n, a[30], s;
int ret;

void rec(bool data[30], int deep)
{
	// cout << deep << endl;
	if (deep == n){
		int a_sum = 0, b_sum = 0;
		for (int i = 0; i < n; i++){
			if (data[i]) a_sum += a[i];
			else b_sum += a[i];
		}

		// cout << sum << endl;
		ret = min(ret, abs(a_sum - b_sum));
		return;
	}
	rec(data, deep + 1);
	data[deep] = true;
	rec(data, deep + 1);
	data[deep] = false;
	return;
}

int main(void)
{
	while (cin >> n, n) {
		ret = 3000000;
		for (int i = 0; i < n; i++){
			cin >> a[i];
		}

		bool data[30];
		fill(data, data + 30, false);
		rec(data, 1);
		data[0] = true;
		rec(data, 1);
		

		cout << ret << endl;
	}

	return 0;
}

C++: #include <iostream>
#define INF (1<<21)
using namespace std;

int n,t[20],mi,a[20];

void rec(int p){
  int a1,a2;
  if(p == n){
    a1 = 0;
    a2 = 0;
    for(int i=0;i<n;i++){
      if(t[i] == 1) a1 += a[i];
      else a2 += a[i];
    }
    if(a1 - a2 > 0 && a1 - a2 < mi) mi = a1 - a2;
    else if(a2 - a1 > 0 && a2 - a1 < mi) mi = a2 - a1;
    return;
  }

    t[p] = 2;
    rec(p+1);
    t[p] = 1;
    rec(p+1);

}

int main(){
  int i,j;
  while(1){
    cin >> n;
    if(n == 0) break;
    for(i=0;i<n;i++){
      cin >> a[i];
      t[i] = 0;
    }

    mi = INF;
    rec(0);
    cout << mi << endl;

  }
  return 0;
}

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

int main(void){
  int n;
  while(cin >> n && n){
    int a[n],sum,ret = 1<<29;
    for(int i=0;i<n;i++) cin >> a[i];
    sum = accumulate(a,a+n,0);
    for(int i=1;i<(1<<n)-1;i++){
      int tmp=0;
      for(int j=0;j<n;j++){
	if(i&(1<<j)) tmp+=a[j];
      }
      ret = min(ret, abs(tmp - (sum-tmp)));
    }
    cout << ret << endl;
  }
}

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

using namespace std;
int abs(int a)
{
	return(a < 0 ? -a : a);
}

void solve()
{
	int n;
	while(cin >> n, n)
	{
		vector<int> Vec(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Vec[i];
		}
		int mi = 999999999;
		int a = 0;
		int b = 0;
		for(int i = 0; i < (1 << n); ++i)
		{
			a = b = 0;
			for(int j = 0; j < n; ++j)
			{
				if((1 << j) & i)
				{
					a += Vec[j];
				}
				else
				{
					b += Vec[j];
				}
			}
			mi = min(mi, abs(a - b));
		}
		cout << mi << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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

int N, n[21];

int dfs(int suma, int sumb, int k){
    int a, b;
    if(k >= N)   return abs(suma - sumb);
    a = dfs(suma+n[k], sumb, k+1);
    b = dfs(suma, sumb+n[k], k+1);
    return min(a, b);
}

int main(void){
    while(cin >> N){
        if(N==0)    break;
        for(int i=0; i<N; i++){
            cin >> n[i];
        }
        cout << dfs(0, 0, 0) << endl;
    }
    return 0;
}