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 <vector>
#include <algorithm>

using namespace std;

int main(int argc, char *argv[]) {
  int pon, gon;
  int n, tmp;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    vector<int> party;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      party.push_back(tmp);
    }
    sort(party.begin(), party.end());
    pon = gon = 0;
    for (int i = 0; i < n; i++) {
      //cout << party[n-1-i];                                                                                                                     
      if (pon >= gon)
        gon += party[n-1-i];
      else
        pon += party[n-1-i];
    }
    cout << (pon-gon < 0 ? gon-pon : pon-gon) << endl;
  }
  return 0;
}

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

using namespace std;

const int MAX = 21;
const int INF = (1<<25);

int in;
int data[MAX];
int mindif;

void dfs(int i, int sum){
  if(i==in){
    mindif = min(mindif,abs(sum));
    return;
  }
  dfs(i+1,sum+data[i]);
  dfs(i+1,sum-data[i]);
}

int main(){
  while(cin >> in && in){
    for(int i = 0; i < in; i++) cin >> data[i];
    mindif = INF;
    dfs(0,0);
    cout << mindif << endl;
  }
  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{
				if(memo[pos].count(sum)) continue;
				else memo[pos].insert(sum);
				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>
#include <algorithm>
#include <cstdlib>

using namespace std;

const int inf = 1000001;

int main()
{
  int n, mn, t, a[21], mask, s, u;

  while(cin>>n && n){
    t = 0;
    mn = inf;

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

    mask = 1<<n;
    for(int i = 0; i < mask; i++){
      s = 0;
      for(int j = 0; j < n; j++){
	u = 1<<j;
	if(i&u) s += a[j];
      }  
      mn = min(mn, abs(s-(t-s)));
    }

    cout << mn << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<cstdlib>
main(){for(int n,a[20],S,s,m,i,j,t;std::cin>>n,n;printf("%d\n",m)){for(i=S=0;i<n;S+=a[i++])std::cin>>a[i];m=1<<30;for(i=(1<<n);i-->0;t=abs(S-s-s),m>t&&(m=t))for(j=s=0;j<n;j++)if(i&1<<j)s+=a[j];}}

C++: #include<cstdio>
#include<cstdlib>
#include<numeric>

using namespace std;

int main(){
	for(int n;scanf("%d",&n),n;){
		int a[20];
		for(int i=0;i<n;i++)	scanf("%d",a+i);

		int ans=1<<30;
		int sum=accumulate(a,a+n,0);
		for(int i=0;i<(1<<n);i++){
			int psum=0;
			for(int j=0;j<n;j++){
				if(i&(1<<j))	psum+=a[j];
			}
			ans=min(ans,abs(sum-2*psum));
		}

		printf("%d\n",ans);
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int n,ans;
int t[20];

void solve(int x,int a,int b){
  if(x==n){
    ans=min(ans,max(a,b)-min(a,b));
  }else{
    solve(x+1,a+t[x],b);
    solve(x+1,a,b+t[x]);
  }
}

int main(){
  while(1){
    cin>>n;
    if(n==0)break;
    for(int i=0;i<n;i++)cin>>t[i];
    ans=(1e9);
    solve(0,0,0);
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>	// require sort next_permutation count etc.
#include <climits>
#include <limits>
//#include <fstream>	// require freopen

using namespace std;

int main()
{
//	cut here before submit 
//	freopen ("testcase.splitup", "r", stdin );
	int n;

	while (cin >> n && n ){
		vector <int> party(n);
		for (int i = 0; i < n; ++i ){
			cin >> party[i];
		} // end for
		sort (party.begin(), party.end(), greater<int>() );

		int termA = 0, termB = 0; 

		termA = party[0];

		if (n == 1 ){
			return termA;
		} // end 

		for (int i = 1; i < n; ++i ){
			if (termA > termB ){
				termB += party[i];
			}else{
				termA += party[i];
			} // end if
		} // end for
		int res = abs (termA - termB );
		cout << res << endl;
	} // end loop
		
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
#include <cstring>
#include <climits>
#include <queue>
#include <map>
#include <sstream>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-9

int main(){
	int n;
	int data[21];
	while(cin>>n,n){
		int ret = INT_MAX;
		rep(i,n)cin>>data[i];
		rep(i,(1<<n)){
			int a=0,b=0;
			for(int j=0; j<n; j++){
				if( i&(1<<j) ){
					a += data[j];
				}else{
					b += data[j];
				}
			}
			ret = min(ret, abs(a-b));
		}
		printf("%d\n",ret);
	}
	return 0;
}