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 <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



int n;
int a[25];

int main() {
    while (cin >> n) {
        if (n == 0) break;
        int sum = 0;
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
            sum += a[i];
        }
        int res = 1<<29;
        for (int bit = 0; bit < (1<<n); ++bit) {
            int tmp = 0;
            for (int j = 0; j < n; ++j) if (bit & (1<<j)) tmp += a[j];
            chmin(res, abs(sum - tmp*2));
        }
        cout << res << endl;
    }
    return 0;
}

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

const int INF = 1 << 30;

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

    vector<int> a(n);
    for (int i = 0; i < n; ++i)
      cin >> a[i];

    int ans = INF;
    for (int i = 0; i < (1 << n); ++i) {
      int p = 0, b = 0;
      for (int j = 0; j < n; ++j) {
	if (i & (1 << j))
	  p += a[j];
	else
	  b += a[j];
      }
      ans = min(ans, abs(p-b));
    }

    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

int n;
int memo = -1;
int *sa, *sb, *sc, *a;

int bit(int m) {
	if (memo == -1) {
		int ans = 1;
		for (int i = 0; i < m; i++)
			ans *= 2;
		return ans;
	}
	else
		return memo;
}

int main() {
	while (1) {
		cin >> n;
		if (n == 0)
			break;
		sa = new int[bit(n)];
		sb = new int[bit(n)];
		sc = new int[bit(n)];
		a = new int[n];
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < bit(n); i++) {
			for (int j = 0; j < n; j++) {
				if (i >> j & 1)
					sa[i] += a[j];
				else
					sb[i] += a[j];
			}
			sc[i] = abs(sa[i] - sb[i]);
		}
		int min = 1000000;
		for (int i = 0; i < bit(n); i++) {
			if (min > sc[i])
				min = sc[i];
		}
		cout << min << endl;


	}


	return 0;
}

C++: #include <iostream>
#include <stdlib.h>
#include <algorithm>
#define uint unsigned int

using namespace std;

int main(int argc, char **argv)
{
	uint *a, g, p, n;
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		} else if(n == 1){
			cin >> p;
			cout << p << endl;
			continue;
		}
		a = (uint *)calloc(n, sizeof(uint));
		
		for(int i = 0; i < n; i++){
			cin >> p;
			a[i] = p;
		}
		
		p = g = 0;
		sort(a, a+n);
		for(int i = n-1; i >= 0; i--){
			if(p > g){
				g += a[i];
			} else {
				p += a[i];
			}
		}
		p -= g;
		cout << ((p < 0)? -p : p) << endl; 
		
		free(a);
	}
	
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int a,b,x,c[100];
void mutu(int y,int z,int zz){
    if(y+z==b){
        if(x>abs(y-z)) x=abs(y-z);
    }
    else{
        mutu(y+c[zz],z,zz+1);
        mutu(y,z+c[zz],zz+1);
    }
}
int main(){
    while(1){
        x=1000000;
        b=0;
    cin>>a;
    if(a==0) break;
    for(int i=0;i<a;i++){
        cin>>c[i];
        b+=c[i];
    }
    mutu(0,0,0);
    cout<<x<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cmath>
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;
}