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 <cmath>

using namespace std;

int n, a[30], s;
int ret;

void rec(bool data[30], int deep)
{
	// cout << deep << endl;
	if (deep == n){
		int a_sum = 0, b_sum = 0;
		for (int i = 0; i < n; i++){
			if (data[i]) a_sum += a[i];
			else b_sum += a[i];
		}

		// cout << sum << endl;
		ret = min(ret, (int)abs(a_sum - b_sum));
		return;
	}
	rec(data, deep + 1);
	data[deep] = true;
	rec(data, deep + 1);
	data[deep] = false;
	return;
}

int main(void)
{
	while (cin >> n, n) {
		ret = 3000000;
		for (int i = 0; i < n; i++){
			cin >> a[i];
		}

		bool data[30];
		fill(data, data + 30, false);
		rec(data, 1);
		data[0] = true;
		rec(data, 1);
		

		cout << ret << endl;
	}

	return 0;
}

C++: #include<iostream>
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=S-s-s,t=t>0?t:-t,m>t&&(m=t))for(j=s=0;j<n;j++)if(i&1<<j)s+=a[j];}}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#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){
		int dp2[21] = {0};
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear();
		for(int i = n-1 ; i >= 0 ; i--) dp2[i] = dp2[i+1] + v[i];
		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( sum >= dp2[pos] && ans <= sum-dp2[pos]  ) continue;
				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<bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
int a;
while(cin>>a,a){
	int sum=0;
	vector<vector<int>>c(a+1);
	c[0].push_back(0);
	for(int b=0;b<a;b++){
		int e;
		scanf("%lld",&e);
		sum+=e;
		for(int d=0;d<c[b].size();d++){
			c[b+1].push_back(c[b][d]);
			c[b+1].push_back(c[b][d]+e);
		}
	}
	sort(c[a].begin(),c[a].end());
	auto t=upper_bound(c[a].begin(),c[a].end(),(double)sum/2);
	auto qq=t;
	qq--;
printf("%d\n",min(abs(*t-(sum-*t)),abs(*qq-(sum-*qq))));
}
}

C++: #include<iostream>
#include<climits>
using namespace std;
#define MAX 20

int T[MAX], n, sum, minv;

void rec( int cur, int v ){
    if ( cur == n ){
	int d = max(v, sum-v) - min(v, sum-v);
	minv = min(minv, d);
	return;
    }
    rec( cur+1, v);
    rec( cur+1, v+T[cur]);
}

void compute(){
    minv = INT_MAX;
    rec(0, 0);
    cout << minv << endl;
}

main(){
    while( cin >>n && n ) {
	sum = 0;
	for ( int i = 0; i < n; i++ ){
	    cin >> T[i]; sum += T[i];
	}
	compute();
    }
}

C++: #include<bits/stdc++.h>

using namespace std;

int calc_diff(vector<int>& strength, int team) {

  int team_a = 0, team_b = 0;

  for (int i = 0; i < (int) strength.size(); i++) {
    if (team & 1) {
      team_a += strength[i];
    } else {
      team_b += strength[i];
    }
    team >>= 1;
  }

  return abs(team_a - team_b);
}

int main() {

  int size;
  while (cin >> size && size != 0) {

    vector<int> strength(size);
    for (int i = 0; i < size; i++) {
      cin >> strength[i];
    }

    int minDiff = INT_MAX;
    for (int i = 0; i < (1<<size); i++) {
      minDiff = min(minDiff, calc_diff(strength, i));
    }

    cout << minDiff << endl;
  }
  
  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,dif=1e+9;
		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];
			dif=min(dif,abs(s[i]*2-sum));
		}
		cout<<dif<<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 <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int n;
int dfs(int i, vector<int> a, int sum, int max){
  if(i==n) return abs(max-sum*2);
  if(sum*2>max) return abs(max-2*sum);
  return min( dfs(i+1,a,sum+a[i],max), dfs(i+1,a,sum,max));
}
int main(void){
  while(cin>>n){
    if(n==0) break;
    vector<int> a(n);
    REP(i,n) cin>>a[i];
    int s = accumulate(a.begin(),a.end(),0);
    cout<<dfs(0,a,0,s)<<endl;
  }
  return 0;
}