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>
using namespace std;

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<")"<< endl
#define BINARY(x) static_cast<bitset<16> >(x)

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

#define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()

typedef long long ll;
const int INF   = 1e9;
const ll  INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};

int main()
{
    ios::sync_with_stdio(false);

    int N;
    while (cin >> N, N) {
        vector<int> A(N);
        rep(i, N) cin >> A[i];
        ll ans = INFLL;

        for (int g=0; g<(1<<N); g++) {
            ll sum1 = 0, sum2 = 0;
            for (int i=0; i<N; i++) {
                (g>>i & 1 ? sum1 : sum2) += A[i];
            }
            ans = min(ans, abs(sum1 - sum2));
        }

        cout << ans << endl;
    }
}

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

int dfs(int n,int x[20],int count,int N,int sum){
  if(n==N){
    return abs(sum-(count-sum));
  }else{
    return min(dfs(n,x,count,N+1,sum+x[N]),dfs(n,x,count,N+1,sum));
  }
}

int main(){
  int n;
  int x[20];
  while(1){
    cin >> n;
    if(n==0){
      return 0;
    }
    int count=0;
    for(int i=0;i<n;i++){
      cin >> x[i];
      count += x[i];
    }
    cout << dfs(n,x,count,0,0) <<endl;
  }
}

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

using namespace std;

int main(){

	int n;
	int a[20];
	while(cin >> n && n != 0){
		for(int i = 0; i < n; i++){
			cin >> a[i];
		}
		int minNum=1000000000;
		for(int s = 0; s < (1 << n); s++){
			int A,B;
			A=B=0;
			for(int j = 0; j < n; j++){
				if(1 & (s >> j)){
					A+=a[j];
				}
				else{
					B+=a[j];
				}
			}
			minNum=min(minNum,abs(A-B));
		}
		cout << minNum << endl;
	}

	return 0;
}

C++: #include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

int data[100]={};
int mini;
int n;
int SUM;


void dfs(int num,int sum){
    if(num>=n){
        mini=min(mini,abs(SUM-sum-sum));
        return ;
    }
    
    dfs(num+1,sum+data[num]);
    dfs(num+1,sum);
}


int main() {
    while(cin>>n){
        if(n==0)break;
        
        SUM=0;
        mini=99999999;
        rep(i,100) data[i]=0;
        
        rep(i,n){
            cin>>data[i];
            SUM+=data[i];
        }
        
        dfs(0,0);
        cout<<mini<<endl;
    }
    
    return 0;
}

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

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

int solve(int i, int A, int B){
	if( i == n ){
		ans = min( ans , abs(A-B) );
	}else{
		solve( i+1 , A+a[i] , B );
		solve( i+1 , A , B+a[i] );
	}
}

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

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

using namespace std;

int main(){

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

    int data[in];
    int sum[2] = {0,0};
    int suma[2] ={0,0};
    int n[in];

    for(int i = 0; i < in; i++){
      cin >> n[i];
      
    }
    sort(n, n+in);

    for(int i = in-1; i >= 0; i--){
      if(sum[0] > sum[1]) sum[1]+=n[i];
      else sum[0]+=n[i];
    }

    reverse(n,n+in);

    for(int i = in-1; i >= 0; i--){
      if(suma[0] > suma[1]) suma[1]+=n[i];
      else suma[0]+=n[i];
    }

    sum[0] -= sum[1];
    suma[0] -= suma[1];

    if(sum[0] < 0) sum[0]*=-1;
    if(suma[0] < 0) suma[0]*=-1;

    if(sum[0] > suma[0]) sum[0] = suma[0];

    cout << sum[0] << endl;
  }
  return 0;
}

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

int diff( int a, int b )
{
    int c = a - b;
    return c < 0 ? -c : c;
}

int main()
{
    int n;
    bitset<20> b;

    while( cin >> n )
    {
        if( !n )
            break;

        int* a = new int[n];

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

        int limit = 1;
        for( int i = 0; i < n; i++ )
            limit *= 2;

        int min = 1000000000;
        for( int i = 1; i < limit; i++ )
        {
            b = i;
            int s = 0;
            for( int i = 0; i < n; i++ )
                if( b[i] )
                    s += a[i];
            int d = diff( s, sum - s );
            if( min > d )
                min = d;
        }
        
        cout << min << endl;
        delete a;
    }
    return 0;
}