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<cstdio>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<complex>
#include<stack>
using namespace std ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<string> vs ;
typedef pair<int, int> pii; 
typedef long long ll ;

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

int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int main(void){
  int n;
  while(cin>>n,n){
    
    vector<int> vec(n);
    rep(i,n)cin>>vec[i];
    int ans=1000000;
    vector<int> f,s;
    rep(j,1<<n){
      int a=0,b=0;
      rep(i,n){
	//cout<<j<<endl;
	if((j>>i)%2)a+=vec[i];
	else b+=vec[i];
      }
      ans= min(ans,abs(a-b));
      //cout<<ans<<endl;
    }
    cout<<ans<<endl;
    
    

    
  }
}

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

using namespace std;

int main(){

	int n;
	while(cin>>n&&n!=0){
		int a[20];
//		int allsum=0;
		for(int i=0; i < n; i++){
			cin >> a[i];
//			allsum+=a[i];
		}
		sort(a,a+n);

		int ret=10000000;
		for(int s = 0; s < (1 << n); s++){
			int sum=0;
			int left=0;
			for(int i = 0; i < n; i++){
				if(1 & (s>>i)){
					sum+=a[i];
				}
				else
					left+=a[i];
			}
			ret=min(ret,abs(sum-left));
			if(ret==0){
				break;
			}
		}
		cout<<ret<<endl;

		//int Asum=0;
		//vector<int>A,B;
		//int minSum;
		//for(int i = 0; i < n; i++){
		//	minSum = abs(Asum-sum);
		//	if(Asum < sum){
		//		A.push_back(a[i]);
		//		Asum+=a[i];
		//		sum-=a[i];
		//	}
		//	else{
		//		for(int j = i; j < n; j++){
		//			B.push_back(a[j]);
		//		}
		//		break;
		//	}
		//}
		//minSum = abs(Asum-sum);
		//for(int i = 0; i < A.size(); i++){
		//	if(Asum <= sum)
		//		break;
		//	Asum-=A[i];
		//	sum+=A[i];
		//	minSum = min(minSum,abs(Asum-sum));
		//}
		//cout << minSum << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a,c) memset(a,c,sizeof(a))

#define DEBUG(x) cout<<"#x"<<": "<<x<<endl

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = INT_MAX/3;
const ll MOD = 1000000007;
const double EPS = 1e-14;
const int dx[] = {1,0,-1,0} , dy[] = {0,1,0,-1};

int a[30];
int n;

int dfs(int x,int b,int c){
    if(x == n) return abs(b-c);
    int res = min(dfs(x+1,b+a[x],c) , dfs(x+1,b,c+a[x]));
    return res;
}

int main(){

    while(true){
	cin >> n;
	if(n == 0) break;
	REP(i,n){
	    cin >> a[i];
	}
	cout << dfs(0,0,0) << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#include<bitset>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
/*string st(int bina,int n){
  string out;
  for (int i = 0; bina>0 ; i++){
    out = (char)((bina%2)+'0')+out;
    bina = bina/2;
  }
  while(out.size()!=n)out='0'+out;
  return out;
}  */
int main(){
  int n;
  while(cin>>n,n){
    int ans=9999999;
    vector<int>in(n);
    int sum2=0;
    rep(i,n){cin>>in[i];sum2+=in[i];}
    rep(i,1<<n){
      int sum=0;
      rep(j,n)if((i>>j)&1)sum+=in[j];
      if(ans>abs(2*sum-sum2))ans=abs(sum2-2*sum);
      //cout<<i<<" "<<sum<<endl;
    }
    cout<<ans<<endl;
  }

}

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

using namespace std;

int n, mem_index = 0, fight_strength[20] = {0}; //登録者数、何番目の登録か、登録者の戦闘力

int my_min(int now, int a_sum, int b_sum)
{
	if(now == n) return abs(a_sum - b_sum);

	int min_a = my_min(now + 1, a_sum + fight_strength[now], b_sum),
	    min_b = my_min(now + 1, a_sum, b_sum + fight_strength[now]);

	if(min_a > min_b)
		return min_b;
	else if(min_a < min_b)
		return min_a;
	else
		return min_a;
}

int main(void)
{
	while(true)
	{
		cin >> n;
		if(n == 0) break;
		while(mem_index < n)
		{
			cin >> fight_strength[mem_index];
			mem_index++;
		}

		cout << my_min(0, 0, 0) << endl;
		mem_index = 0;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
 
int a[20];
vector<int> b,c;
int m;
int minsa;
 
int co(){
  int sum=0;
  for(int i=0;i<b.size();i++) sum+=b[i];
  for(int i=0;i<c.size();i++) sum-=c[i];
  if(sum<0) sum*=-1;
  return sum;
}
 
void dps(int u){
 
  if(u==m){
    int sa=co();
    if(sa<minsa) minsa=sa;
    else if(minsa==-1) minsa =sa;
  }
  else {
    b.push_back(a[u]);
    dps(u+1);
    b.pop_back();
    c.push_back(a[u]);
    dps(u+1);
    c.pop_back();
  }
}
 
 
int main(){
   
  while(1){
    cin >> m;
    if(m==0) break;
    
    for(int j=0;j<m;j++) cin >>a[j];
      minsa =-1;
      dps(0);
      cout << minsa << endl;
  }
 
  return 0;
}