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>
#include <cstdlib>
#include <climits>

using namespace std;

int n, map[20], Min;

void search( int A, int B, int count )
{
	if(count == n) {
		Min = min(Min, abs(A-B));
		return;
	}
	search(A + map[count], B, count + 1);
	search(A, B + map[count], count + 1);
}

int main( void )
{
	while(cin >> n, n) {
		for (int i = 0; i < n; ++i) cin >> map[i];
		Min = INT_MAX;
		search(0, 0, 0);
		cout << Min << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
 
const int INF = 1000000;


int main(){
	int n;
	int s = 0, t = 0;
	while(cin >> n && n > 0){
		s = 0, t = 0;
		vi v(n, 0);
		rep(i, n) cin>>v[i];
		sort(all(v));
		reverse(all(v));
		rep(i, n){
			if(t > s) s += v[i];
			else t += v[i];
		}

		//cout<<s<<" "<<t<<endl;

		cout<<abs(s - t)<<endl;
	}
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
#define INF (1<<25)
 
int main(){
	int n , v[20] , X[41] , Y[41] , top;
	while(scanf("%d\n",&n)&& n){
		int dp2[21] = {0};
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear();
		sort(v,v+n,greater<int>());
		for(int i = n-1 ; i >= 0 ; i--) dp2[i] = dp2[i+1] + v[i];
		
		int ans = INF;
		
		top = 0;
		X[top] = 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 <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
 
const int INF = 1000000;


int main(){
	int n;
	int s = 0, t = 0;
	while(cin >> n && n > 0){
		s = 0, t = 0;
		vi v(n, 0);
		rep(i, n) cin>>v[i];
		sort(all(v));
		reverse(all(v));
		rep(i, n){
			if(t > s) s += v[i];
			else t += v[i];
		}

		cout<<s<<" "<<t<<endl;

		cout<<abs(s - t)<<endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) REP(i,0,n)
#define INF (1<<30)
#define INFLL (1LL<<62LL)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

int solve(vector<int> & A, int a, int b, int idx) {
  if (idx == A.size())
    return abs(a - b);
  return min(solve(A, a + A[idx], b, idx + 1), solve(A, a, b + A[idx], idx + 1));
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  int N;
  while (cin >> N, N) {
    vector<int> A(N);
    rep(i, N) cin >> A[i];

    cout << solve(A, 0, 0, 0) << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int table[20];
int main(){
	int a;
	while(scanf("%d",&a),a){
		int sum=0;
		for(int i=0;i<a;i++){
			scanf("%d",table+i);
			sum+=table[i];
		}
		int ret=99999999;
		for(int i=0;i<(1<<a);i++){
			int val=0;
			for(int j=0;j<a;j++){
				if(i&(1<<j))val+=table[j];
			}
			ret=min(ret,abs(sum-val-val));
		}
		printf("%d\n",ret);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef set<int> S;

int main(){
  int n;
  while(cin>>n,n){
    int a[20],sum=0;
    for(int i=0;i<n;i++)cin>>a[i],sum+=a[i];
    
    S mp[21];
    mp[0].insert(0);
    for(int i=0;i<n;i++)
      for(S::iterator it=mp[i].begin();it!=mp[i].end();it++)mp[i+1].insert((*it)+a[i]);
    
    int ans=1e9;
    for(S::iterator it=mp[n].begin();it!=mp[n].end();it++)ans=min(ans,sum-(*it));
    cout <<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<climits>
#include<cmath>
#include<algorithm>
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;
    }
    
}