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<stdio.h>
#include<stdlib.h>
int main(){
  int n,m,k,i,j,a,b;
  int d[20];
  while(1){
    scanf("%d",&n);
    if(n==0)break;
    int min=1000001;
    for(i=0;i<n;i++){
      scanf("%d",&d[i]);
    }
    //k=1;
    //for(i=0;i<n;i++)k*=2;
    for(i=0;i<(1<<n);i++){
      a=b=0;
      m=i;
      for(j=0;j<n;j++){
	if(m%2)a+=d[j];
	else   b+=d[j];
	m/=2;
      }
      if(min>abs(a-b)){
	min=abs(a-b);
      }
    }
    printf("%d\n",min);
  }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <tr1/unordered_set>
using namespace std;

int n , v[20] , ans = 0;

tr1::unordered_set<int> memo[20];

int X[421] , Y[421] , top;
 
int main(){
	while(scanf("%d\n",&n)&& n){
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear() ;
		ans = 99999999;
		top = 0;
		X[top] = 0;
		Y[top] = 0;
		while(~top){
			int pos = X[top] , sum = Y[top]; top--;
			if(pos==n) ans = min(ans,sum);
			else{
				X[++top] = pos+1;
				Y[top]   = sum+v[pos];
				X[++top] = pos+1;
				Y[top]   = abs(sum-v[pos]);
			}
		}
		printf("%d\n",ans);
	}
}

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] = 1;
  rec(p+1);
  t[p] = 2;
  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<climits>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>

using namespace std;

int main(){
    int n;
    int a[22];
    while(cin>>n,n){
        for(int i=0;i<n;i++)
            cin>>a[i];
        unsigned long long l=0;
        int mi=INT_MAX;
        while(l<(1<<n)){
            int p=0,q=0;
            for(int i=0;i<n;i++){
                if((l>>i)&1)
                    p+=a[i];
                else
                    q+=a[i];
            }
            mi=min(abs(q-p),mi);
            l++;
        }
        cout<<mi<<endl;
    }
    
}

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;
		party0 = party1 = 0;
		all = 0;
	}
	return 0;
}

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

using namespace std;

int main () {


	int ab[20];
	bool used[20];
	int sum;
	int aSum;
	int bSum;
	bool aUsed[20];
	bool bUsed[20];

	while (true) {
	
		int n;
		cin >> n;
		if ( n == 0)
			break;
		sum = 0;
		aSum = 0;
		bSum = 0;
		for (int i=0; i<n; i++ ) {
			cin >> ab[i];
			used[i] = false;
			aUsed[i] = false;
			bUsed[i] = false;
			sum += ab[i];
		}

		sort ( ab, &ab[n]);

		for ( int i=n-1; i>=0; i-- ) {
			if ( aSum <= bSum ) {
				aSum += ab[i];
				aUsed[i] = true;
			} else {
				bSum += ab[i];
				bUsed[i] = true;
			}
		}
		/*
		bool stop = true;
		while (true) {
			if (abs (aSum - bSum) / 2 == 0 ) {
				break;
			}

			for ( int i=0; i<n; i++ ) {
				if ( abs(aSum - bSum)/2 >= ab[i] ) {
					if ( aUsed > bUsed ) {

			}
		}
				*/
		cout << abs(aSum - bSum)<< endl;
	}


}

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 21;

int a[MAX],n,res;

int dfs(int num,int x,int y){
  if(num == n){
    if(x < y){
      res = min(res,y-x);
    }
    else{
      res = min(res,x-y);
    }
  }
  else{
    dfs(num+1,x+a[num],y);
    dfs(num+1,x,y+a[num]);
  }
}

int main(){
  while(cin >> n ,n){
    for(int i = 0 ; i < n ; i++){
      cin >> a[i];
    }
    res = 1<<28;
    dfs(0,0,0);
    cout << res << endl;
  }

  return 0;
}

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

int n, arr[20];

int dfs(int m, int d){
    if(m == n) return abs(d);
    int pat1 = dfs(m + 1, d + arr[m]);
    int pat2 = dfs(m + 1, d - arr[m]);
    return min(pat1, pat2);
}

int main(){
    scanf("%d", &n);
    for(int x = 0; x < n; x++){
        scanf("%d", arr+x);
    }
    printf("%d\n", dfs(0, 0));
    return 0;
}

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

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

int main(){
    int n;
    int a[100];
    while(cin>>n && n){
        rep(i,n)cin>>a[i];
        int ans=1<<20;
        rep(i,1<<n){
            int d=0;
            rep(j,n)if(i>>j&1)d+=a[j];else d-=a[j];
            ans=min<int>(ans,abs(d));
        }
        cout<<abs(ans)<<endl;
    }
}