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<bits/stdc++.h>
#define mk make_pair
using namespace std;
int a[20],n;
int dfs(int d,int x,int y){
  if(d==n){if(y<x)swap(x,y);return y-x;}
  return min(dfs(d+1,x+a[d],y),dfs(d+1,x,y+a[d]));
}
int main(){
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>a[i];
    cout<<dfs(0,0,0)<<endl;
  }
}

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

using namespace std;

vector<int> Vec;
int n;
int Ans;
static const int INF = 1e+9;

void DFS(int a, int b, int depth)
{
	if(depth == n)
	{
		Ans = min(Ans, abs(a - b));
	}
	else
	{
		DFS(a + Vec[depth], b, depth + 1);
		DFS(a, b + Vec[depth], depth + 1);
	}
}

void solve()
{
	while(cin >> n, n)
	{
		Vec.resize(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Vec[i];
		}
		Ans = INF;
		DFS(0, 0, 0);
		cout << Ans << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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

using namespace std;

vector<int> ans;
int main(){
	while(1){
		int n;
		cin >> n;
		
		if(n == 0) break;
		
		int a = 0,b = 0;
		int nl[20];
		for(int i = 0;i < n;i++){
			cin >> nl[i];
		}
		
		sort(nl,nl+n);
		reverse(nl,nl+n);
		
		for(int i = 0;i < n;i++){
			if(a > b){
				b += nl[i];
			}else{
				a += nl[i];
			}
		}
		ans.push_back(abs(a-b));
	}
	for(int i = 0;i < ans.size();i++) cout << ans[i] << endl;
	
	return 0;
}

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

using namespace std;

int main(){
    int n;
    int a[22];
    while(cin>>n,n){
        for(int i=0;i<n;i++)
            cin>>a[i];
        int mi=INT_MAX;
        for(int l=0;l<(2<<n);l++){
            int p=0,q=0;
            for(int i=0;i<n;i++){
                if((l>>i)&1)
                    p+=a[i];
                else
                    q+=a[i];
            }
            mi=min(abs(q-p),mi);
        }
        cout<<mi<<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;
int FightingPower[MAX_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;
	}
}

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

int n;
long m, sum, a[20];

//sum/2??\?????§????????????????????????????¨?
void rec(int x, long y){ //x???????????\???????????\????????????,???????????????y
	if(x==n){
		if(m<y) m=y;	
	}
	else{
		if(y+a[x]>sum/2) rec(x+1,y);
		else{
			rec(x+1,y+a[x]);
			rec(x+1,y);		
		}	
	}
	
}

int main(){
	while(1){
		scanf(" %d", &n);
		if(n==0) break;
		
		sum=0;
		for(int i=0; i<n; ++i){
			scanf(" %ld", &a[i]);
			sum+=a[i];
		}
	
		m=0;	
		rec(0, 0);
		
		long mm=sum-m;
		//printf("m=%ld, mm=%ld\n",m ,mm);
		printf("%ld\n", mm-m);
	}
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<complex>
#include<stack>
using namespace std ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<string> vs ;
typedef pair<int, int> pii; 
typedef long long ll ;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()

int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int main(void){
  int n;
  while(cin>>n,n){
    
    vector<int> vec(n);
    rep(i,n)cin>>vec[i];
    int ans;
    vector<int> f,s;
    rep(j,1<<n){
      int a,b;
      vector<int> aa,bb;;
      rep(i,n){
	if(i%2)a+=vec[i];
	else b+=vec[i];
      }
      a>b?ans=min(a-b,ans):ans=min(b-a,ans);
    }
    cout<<ans<<endl;
    
    

    
  }
}

C++: #include<iostream>
#include<cstdio>
#include<climits>
#include<algorithm>
#include<cmath>
#define N 1000
using namespace std;
int n,C[N],tans;


int ans(int i,int A,int B)
{
  if(i == n)
    {
      tans = min(tans,abs(A-B)); 
    }
  else
    {
      ans(i+1,A+C[i],B);
      ans(i+1,A,B+C[i]);
    }

  return tans;

}


int main()
{
  while(1)
    {
      cin >> n;
      if(n == 0)break;
      for(int i=0;i<n;i++)
	{
	  cin >> C[i];
	}
       tans = INT_MAX;
      cout << ans(0,0,0) << endl;

    }



}

C++: #include <iostream>
#include <queue>
#define abs(a) (a)>0 ? (a) : (-a)

using namespace std;

int main(){
	int n;
	long all = 0, party0 = 0, party1 = 0, diff;
	priority_queue<long> pq;
	cin >> n;
	while (n){
		for (int i = 0; i < n; i++){
			long input;
			cin >> input;
			pq.push(input);
			all += input;
		}
		for (int i = 0; i < n; i++){
			if (party0 > party1){
				party1 += pq.top();
				pq.pop();
			} else{
				party0 += pq.top();
				pq.pop();
			}
		}
		diff = abs(party0 - party1);
		cout << diff << endl;
		cin >> n;
		party0 = party1 = 0;
		all = 0;
	}
	return 0;
}