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 <string>
#include <vector>
#include <functional>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <sstream>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for(ll i = a; i < b; ++i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

int n, max, ans = infi;
int dat[20];

int main(){
	while (std::cin >> n, n){
		ans = infi;
		rep(i, n)std::cin >> dat[i];

		max = (1 << n);
		rep(i, max){
			int sumA = 0, sumB = 0;
			rep(j, n){
				if ((i >> j) & 1)sumA += dat[j];
				else sumB += dat[j];
			}
			ans = std::min(ans, std::abs(sumA - sumB));
		}
		std::cout << ans << std::endl;
	}
	return 0;
}

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

int abs(int a){
  return (a>0)?a:-a;
}

int main(){
  int n,total;
  int a[20];
  set<int> prob;

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

    total=0;
    for(int i=0; i<n; i++)
      cin>>a[i],total+=a[i];

    prob.insert(0);

    set<int> c;
    for(int i=0;i<n;i++){
      copy(prob.begin(), prob.end(), insert_iterator<set<int> >(c, c.end()));
      for(set<int>::iterator it=c.begin();it!=c.end();++it){
	if((*it)+a[i] > total/2 + 1) continue;
	prob.insert((*it)+a[i]);
      }
    }

    cout<<abs (total - (*(--prob.end()))*2 )<<endl;

    prob.clear();
    c.clear();
    //cout<<"total: "<<total<<endl;
  }
}

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

using namespace std;

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


void dfs(int diff,int k);

int main(){
	
	while(cin>>n){

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

void dfs(int diff,int k){
	if(k==n){
		ans=min(ans,abs(diff));
	}else{
		dfs(diff+a[k],k+1);
		dfs(diff-a[k],k+1);
	}
}

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],s1[1<<10];

int main(){
	while(cin>>n){
		if(!n)return 0;
		int n2=n/2;
		fill(s1,s1+(1<<n2),0);
		int sum=0;//,dif=1e+9;
		for(int i=0;i<n;i++){cin>>p[i];sum+=p[i];}
		for(int i=0;i<1<<n2;i++){
			for(int j=0;j<n2;j++)if(i>>j&1)s1[i]+=p[j];
		}
		sort(s1,s1+(1<<n2));
		int ans=1e+9;
		for(int i=0;i<1<<(n-n2);i++){
			int ss=0;
			for(int j=0;j<n-n2;j++)if(i>>j&1)ss+=p[j+n2];
			ans=min(ans,min(abs((*lower_bound(s1,s1+(1<<n2),(sum+1)/2-ss)+ss)*2-sum),abs((*(lower_bound(s1,s1+(1<<n2),(sum+1)/2-ss)-1)+ss)*2-sum)));
		}
		/*for(int i=0;i<1<<n;i++){
			int sss=0;
			for(int j=0;j<n;j++)if((i/(1<<j))%2)sss+=p[j];
			dif=min(dif,abs(sss*2-sum));
		}*/
		cout<<ans<<endl;
		//sort(s,s+(1<<n));
		//for(int i=0;i<1<<n;i++)s[i]=s[i]*2-sum;
		/*int lb=0,ub=1<<n-1,prv=0;
		while(ub-lb>1){
			int mid=(lb+ub)/2;
			if(s[mid]==prv)break;
			if(s[mid]>=0)ub=mid;
			else lb=mid;
			prv=s[mid];
		}
		cout<<abs(s[ub])<<endl;*/
		//cout<<min(*lower_bound(s,s+(1<<n),0),abs(*(lower_bound(s,s+(1<<n),0)-1)))<<endl;
	}
}

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

int n,m,suma,sumb;
int data[21];

void dfs(int a, int b,int cnt){
  suma+=a;
  sumb+=b;
  if(cnt==n){ 
    m=min(m,abs(suma-sumb));
  }else{
    dfs(data[cnt],0,cnt+1);
    dfs(0,data[cnt],cnt+1);
  }
  suma-=a;
  sumb-=b;
}
int main(){
  while(1){
    scanf("%d",&n);
    if(!n) break;
    m=10000000,suma=sumb=0;
    for(int i=0;i<n;i++)
      scanf("%d",&data[i]);
    dfs(0,0,0);
    printf("%d\n",m);
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int n, a[23]; vector<int>v;
void f(int k, int sum) {
	if (k == n)v.push_back(sum);
	else {
		f(k + 1, sum);
		f(k + 1, sum + a[k]);
	}
	return;
}
int main() {
	while (cin >> n, n) {
		v.clear();
		int sum = 0;
		for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; }
		f(0, 0);
		int ans = n;
		for (int i = 0; i < v.size(); i++) {
			int v1 = v[i], v2 = sum - v[i];
			ans = min(ans, abs(v1 - v2));
		}
		cout << ans << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

static const int MAX_MEMBERS = 20;

int Members;

vector<int> power;
int seek = 0;

int FightingPower[MAX_MEMBERS], n, res;
const int INF = 1000001;

void dfs(int i, int a_sum, int b_sum) {
	if (i == n) {
		res = min(abs(a_sum - b_sum), res);
	}
	else {
		dfs(i + 1, a_sum + FightingPower[i], b_sum);
		dfs(i + 1, a_sum, b_sum + FightingPower[i]);
	}
}

int main() {
	while (cin >> n && n!=0) {
		for (int i = 0; i<n; i++) {
			cin >> FightingPower[i];
		}
		res = INF;
		dfs(0, 0, 0);
		cout << res << endl;
	}
}