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

int main(){
  int n, s1,s2;
  int a[50];

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

    sort( a,a+n );

    s1=0;s2=0;
    for( int i=n-1;i>=0;i-- ){
      if( s1>s2 )
	s2 += a[i];
      else
	s1 += a[i];
    }
    cout << abs( s1-s2 ) << endl;

  }

  return 0;
}

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

using namespace std;

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		vector<long long int> d(n);
		long long int a = 0;
		for (int i= 0; i < n; i++) {
			cin >> d[i];
			a += d[i];
		}
		int l = 1<<n;
		long long int MIN = a;
		for (int i = 0; i < l; i++) {
			long long int b = 0;
			for (int j = 0; j < n; j++) {
				if ((i & (1<<j)) != 0) {
					b += d[j];
				}
			}
			long long int c = abs(a-b);
			long long int dd = abs(c-b);
			MIN = min(MIN, dd);
		}
		cout << MIN << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<complex>
#include<math.h>
using namespace std;int main(){int n,k[20];while(true){cin>>n;if(n==0)break;int sum=0;for(int i=0;i<n;i++){cin>>k[i];sum+=k[i];k[i]*=2;}int best=1000000,m=pow(2.0,n+0.0);for(int i=0;i<m;i++){int bit=i;int count=0;for(int j=0;j<n;j++){if((bit>>(j+1))&1==1)count+=k[j];}best=min(best,abs(count-sum));}cout<<best<<endl;}return 0;}

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

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

int sum;
int a[20];

bool bit(unsigned int a, int k) {
  return ((a >> (k + 1)) & 1);
}

int diff(int m, int n) {
  return ((m - n > 0) ? m - n : n - m);
}

int pow(int a, int n) {
  int ans = 1;
  REP(i, n) {
    ans *= a;
  }
  return ans;
}

int solve(int n) {
  int repeat = pow(2, n), min = sum;
  for (unsigned int bitset = 0; bitset < repeat; bitset++) {
    int agroup = 0;
    REP(k, n) {
      if (bit(bitset, k))
        agroup += a[k];
    }

    int d = diff(agroup, sum - agroup);
    if (min > d) min = d;
  }

  return min;
}

int main() {
  unsigned int n;
  while (cin >> n and n != 0) {
    sum = 0;
    REP(i, n) {
      cin >> a[i];
      sum += a[i];
    }

    cout << solve(n) << endl;/*
    int k;
    cin >> k;
    cout << bit(n, k) << endl;*/
  }
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value))
template <typename T>
void FILL_(void * ptr, size_t size, T value){
  std::fill((T*)ptr, (T*)ptr+size, value);
}
//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};
const int INF =1e+9;
int a[30];
int n;
int ans;
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);
  }
}

int main()
{
  while(cin>>n,n){
    for(int i = 0;i < n;i++){
      cin >>a[i];
    }
    ans = INF;
    dfs(0,0);
    cout <<ans<<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(){
    while(scanf("%d", &n) && n){
        for(int x = 0; x < n; x++){
            scanf("%d", arr+x);
        }
        printf("%d\n", dfs(0, 0));
    }
    return 0;
}

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];

int main(){
	while(cin>>n){
		if(!n)return 0;
		fill(s,s+(1<<n),0);
		int sum=0;
		for(int i=0;i<n;i++){cin>>p[i];sum+=p[i];}
		for(int i=0;i<1<<n;i++){
			for(int j=0;j<n;j++)if((i/(1<<j))%2)s[i]+=p[j];
		}
		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 <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;
}