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 <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (int)(a); v < (int)(b); ++v)
#define FORE(v, a, b) for(int v = (int)(a); v <= (int)(b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define RS resize
#define CLR clear
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
using namespace std;

void split(string str, char chr, vector< string > *res){
	stringstream ss(str);
	string itm;
	while(getline(ss, itm, chr)){
		(*res).PB(itm);
	}
}

int n;
vector< int > a;

int rec(int x, int y, int i){
	if(i == n) return abs(x - y);
	
	return min(rec(x + a[i], y, i + 1), rec(x, y + a[i], i + 1));
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	while(cin >> n, n){
		a.RS(n);
		REP(i, n){
			cin >> a[i];
		}
		
		cout << rec(0, 0, 0) << endl;
	}
	
	return 0;
}

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

int n,ans,a[21];
const int INF = 1e+9;

int sum(vector<int> A){
	int s=0;
	for(int i=0 ; i < A.size() ; i++ ){
		s += A[i];
	}
	return s;
}

int solve(int i, vector<int> A, vector<int> B){
	if( i == n ){
		ans = min( ans , abs( sum(A) - sum(B) ) );
	}else{
		A.push_back( a[i] );
		solve( i+1 , A , B );
		A.pop_back();
		B.push_back( a[i] );
		solve( i+1 , A , B );
		B.pop_back();
	}
}

int main(){
	while( cin >> n , n ){
		vector<int> A,B;
		for(int i=0 ; i < n ; i++ ){
			cin >> a[i];
		}
		ans = INF;
		solve( 0 , A , B );
		cout << ans << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<math>
using namespace std;
int main(){
	double sum,mostnear;
	bool a[20];
	int n,kyas[20],shiftmax,sssum,ansint,i,j;
	while(1){
		scanf("%d",&n);
		if(n==0)
			break;
		for(i=0;i<n;i++){
			scanf("%d",&kyas[i]);
		}
		sort(kyas,kyas+n);
		sum=0;
		for(i=0;i<n;i++){
			sum+=kyas[i];
		}
		sum/=2;
		mostnear = 2100000000;
		shiftmax = 1<<n;
		for(i=0;i<shiftmax;i++){
			for(j=0;j<n;j++){
				a[j] = i&1<<j;
			}
			sssum=0;
			for(j=0;j<n;j++){
				sssum += a[j]*kyas[j];
			}
			mostnear = mostnear>fabs(sssum-sum) ? fabs(sssum-sum) : mostnear;
		}
		printf("%d\n",(int)(mostnear*2));
	}
}

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

using namespace std;

int main(){
  int in;
  while(cin >> in && in){
    long long  data[in];
    for(int i = 0; i < in; i++) cin >> data[i];
    sort(data,data+in);
    reverse(data,data+in);
    long long sum[2] = {0,0};
    for(int i = 0; i < in; i++){
      if(sum[0] > sum[1]) sum[1] += data[i];
      else sum[0] += data[i];
    }
    if(sum[0] > sum[1]) cout << sum[0]-sum[1];
    else cout << sum[1] - sum[0];
    cout << endl;
    //cout << abs(sum[1]-sum[0]) << endl;
  }
  return 0;
}

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

using namespace std;

int main(){
  long long in;
  while(cin >> in && in){
    long long data[in];
    for(int i = 0; i < in; i++) cin >> data[i];
    sort(data,data+in);
    reverse(data,data+in);
    long long sum[2] = {0,0};
    for(int i = 0; i < in; i++){
      if(sum[0] > sum[1]) sum[1] += data[i];
      else sum[0] += data[i];
    }
    cout << abs(sum[1]-sum[0]) << endl;
  }
  return 0;
}