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 <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n,p[20],s[1<<20];

int main(){
	while(cin>>n){
		if(!n)return 0;
		fill(s,s+(1<<n),0);
		int sum=0;
		for(int i=0;i<n;i++){cin>>p[i];sum+=p[i];}
		for(int i=0;i<1<<n;i++){
			for(int j=0;j<n;j++)if((i/(1<<j))%2)s[i]+=p[j];
		}
		sort(s,s+(1<<n));
		//for(int i=0;i<1<<n;i++)cout<<s[i]*2-sum<<" ";
		int lb=0,ub=1<<n-1;
		while(ub-lb>1){
			int mid=(lb+ub)/2;
			if(s[mid]>=sum/2)ub=mid;
			else lb=mid;
		}
		cout<<abs(s[ub]*2-sum)<<endl;
	}
}

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

int main(void){
	int n;
	while(cin>>n,n){
		int a[20]={},b[20]={};
		int sum = 0;
		for(int i=0;i<n;++i){
			cin>>a[i];
			sum+=a[i];
		}
		
		int d = sum;
		
		bool f;
		do{
			f = false;
			for(int i=0;i<n;++i){
				for(int j=0;j<n;++j){
					if(i==j) continue;
					if(abs(sum/2-(d-a[i]+b[j]))<abs(sum/2-d)){
						f=true;
						d-=a[i]-b[j];
						int tmp=a[i];
						a[i]=b[j];
						b[j]=tmp;
					}
				}
			}
		}while(f);
		cout<<abs(sum-d-d)<<endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
           
#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;


int main()
{
    int n;
    while (scanf("%d", &n), n)
    {
	int a[32];
	rep (i, n)
	    scanf("%d", a + i);

	int res = 1 << 26;
	for (int i = 0; i < 1 << n; ++i)
	{
	    int t = 0;
	    for (int j = 0; j < n; ++j)
	    {
		if (i >> j & 1)
		    t += a[j];
		else
		    t -= a[j];
	    }
	    min_swap(res, abs(t));
	}
	printf("%d\n", res);
    }
}

C++: #include<iostream>
using namespace std;
int n,pt[20],dif;
int abs(int a){
    if(a<0)a*=-1;
    return a;
}
void dfs(int atnum,int a,int b){
    if(atnum==n){
        dif=min(dif,abs(a-b));
        return;
    }
    dfs(atnum+1,a+pt[atnum],b);
    dfs(atnum+1,a,b+pt[atnum]);
}
int main(){
    while(cin>>n,n){
        dif=100000000;
        for(int i=0;i<n;i++)cin>>pt[i];
        dfs(0,0,0);
        cout<<dif<<endl;
    }
}

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

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

int main(){
    int n;
    while(cin >> n, n){
        vi v(n);
        int sum = 0, ans = inf;
        rep(i, n){ cin >> v[i]; sum += v[i]; }
        rep(i, 1<<n){
            int suma = 0, sumb = 0;
            rep(j, n){
                if((i>>j)%2) suma += v[j];
                else sumb += v[j];
            }
            ans = min(ans, abs(suma-sumb));
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;

bool flg(int x, int y) {
	if (x&(1 << y))return true;
	else return false;
}

int main() {
	int n;
	while (cin >> n, n) {
		int p[20];
		int sum = 0;
		for (int i = 0;i < n;i++) {
			cin >> p[i];
			sum += p[i];
		}
		int ans = 1.8e9;
		for (int i = 0;i < (1 << n);i++) {
			int a = 0;
			for (int j = 0;j< n;j++) {
				if (flg(i, j))a += p[j];
			}
			ans = min(ans, (abs(a - (sum - a))));
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#define abs(a) (a)>0 ? (a) : (-a)

using namespace std;

int main(){
	int n;
	long all = 0, party0 = 0, party1 = 0, diff;
	priority_queue<long> pq;
	cin >> n;
	while (n){
		for (int i = 0; i < n; i++){
			long input;
			cin >> input;
			pq.push(input);
			all += input;
		}
		for (int i = 0; i < n; i++){
			if (party0 > party1){
				party1 += pq.top();
				pq.pop();
			} else{
				party0 += pq.top();
				pq.pop();
			}
		}
		diff = abs(party0 - party1);
		cout << diff << endl;
		cin >> n;
	}
	return 0;
}