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

#define reps(i,f,n) for(int i = f; i < int(n); i++)
#define rep(i,n) reps(i,0,n)

using namespace std;


int main(void){
	int n;
	while(cin >> n){
		int ans = 100000000;
		int a[32]={0};
		if(!n)break;
		rep(i,n)cin >> a[i];
		
		rep(i,1<<n){
			int ga = 0,gb = 0;
			for(int j = 1,index = 0; j < 1<<n; j = j<<1,index++){
				if(i&j){
					ga+=a[index];
				}else{
					gb+=a[index];
				}
			}
			ans = min(abs(ga-gb), ans);
		}
		
		printf("%d\n", ans);
	}
}

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 <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

#define FOR(i,b,n) for(int i=b;i<n;i++)
#define RFOR(i,b,n) for(int i=n-1;i>=b;i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define LIMIT 10000002
using namespace std;

static const double EPS = 1e-9;
typedef long long ll;
typedef pair<int, int> paii;

int n, allie[1000] = {0};

int solve()
{
	int res = INT_MAX, p = 0;
	bool dp[1000010] = {0};
	
	FOR(j, 0, n)
	{
		p += allie[j];
		
		RFOR(i, 0, 1000010)
		{
			if(( i == 0 || dp[i] == true)&&(i+allie[j] < 1000010))
			{
				dp[i+allie[j]] = true;
			}
		}
	}
	
	FOR(i, 0, p+1)
	{	
		if(dp[i] == true)
			res = min(res, abs((p-i)-i));
	}
	
	return res;
	
}

int main()
{
	while(cin >> n, n)
	{
		FOR(i, 0, n)
		{
			cin >> allie[i];
		}
		
		cout << solve() << endl;
	}
	
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <complex>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		int sum = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
		}
		int res = 1000000000;
		rep(i, (1<<n)) {
			int A = 0;
			rep(j, n) if (i & (1 << j)) A += a[j];
			res = min(res, abs(A - (sum - A)));
		}
		cout << res << 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],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)/2-ss)+ss)*2-sum),abs((*(lower_bound(s1,s1+(1<<n2),(sum)/2-ss)-1)+ss)*2-sum)));
		}
		/*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<<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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> p;
int n, a[50], sum;

/* sum : i<k テつづδ愿つづδつづδ径[i] テつづδ古つ組テつづδ敕つ催つつづδュテつづつケテつづδ古つ和 */
void cmb( int k, int sum ){
  if( k==n ){
    p.push_back( sum );
    return;
  }

  cmb( k+1,sum+a[k] );
  cmb( k+1,sum      );
}


int main(){
  int b,c,d;
  while( cin>>n && n ){
    sum=0;
    for( int i=0;i<n;i++ ){
      cin >> a[i];
      sum += a[i] ;
    }

    p.clear();
    cmb( 0,0 );
    sort( p.begin(),p.end() );

    int min=sum;
    for( unsigned int i=0;i<p.size();i++ ){
      b=p[i];
      c=sum-p[i];
      d=abs( b-c );
      if( d<min )min=d;
    }
    cout << min << endl;
  }

  return 0;
}