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 <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( ans < abs(sum-dp2[pos]) || memo[pos].count(sum)) continue;
				else memo[pos].insert(sum);
				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<vector>
#include<map>

using namespace std;

int n;
int memo = -1;
int *sa, *a;

int bit(int m) {
	if (memo == -1) {
		int ans = 1;
		for (int i = 0; i < m; i++)
			ans *= 2;
		return ans;
	}
	else
		return memo;
}

int main() {
	while (1) {
		cin >> n;
		if (n == 0)
			break;
		sa = new int[bit(n)];
		a = new int[n];
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < bit(n); i++) {
			sa[i] = 0;
			for (int j = 0; j < n; j++) {
				if (i >> j & 1)
					sa[i] += a[j];
				else
					sa[i] -= a[j];
			}
			sa[i] = abs(sa[i]);
		}
		int min = 1000000;
		for (int i = 0; i < bit(n); i++) {
			if (min > sa[i])
				min = sa[i];
		}
		cout << min << endl;
	}


	return 0;
}

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

using namespace std;

void solve()
{
	int n;
	while(cin >> n, n)
	{
		vector<int> Vec(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Vec[i];
		}
		sort(Vec.begin(), Vec.end(), greater<int>());
		int a = Vec[0];
		int b = 0;
		for(int i = 1; i < n; ++i)
		{
			a < b ? a += Vec[i] : b += Vec[i];
		}
		cout << abs(a - b) << endl;
	}
}

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

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

using namespace std;


int a[100];
int minimum = 1000000000, n, sum;
void dfs(int now,int i){
  if(i == n){
    if(minimum > abs(sum-now*2)){
      minimum = abs(sum-now*2);
      return;
    }
  }
  dfs(now, i+1);
  dfs(now-a[i], i+1);
}
int main(){
  while(1){
    sum = 0;
    cin >> n;
    if(n == 0) break;

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

    sort(a, a+n);

    dfs(sum, 0);

    cout << minimum << endl;
  }

  return 0;
}

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

int main() {
  int n;
  while(cin >> n && n) {
    vector<int> a;
    a.push_back(0);
    double h=0;
    for(int i=0; i<n; i++) {
      int x;
      cin >> x;
      h+=x;
      for(int i=a.size()-1; i>=0; i--) a.push_back(a[i]+x);
    }
    h/=2;
    double max=1000000000;
    for(int i=0; i<a.size(); i++) {
      if(max>abs(h-(double)a[i])) max=abs(h-a[i]);
    }
    int ans=max*2;
    cout << ans << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#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>
#include <cstring>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;


int main(){
	int n;
	
	while(cin>>n){
		if(n==0)break;
		
		int mem[n],min=0;
		
		for(int i=0;i<n;i++){
			cin>>mem[i];
			min+=mem[i];
		}
		
		for(int i=0;i<(1<<n);i++){
			int po=0,go=0;
			
			for(int j=0;j<n;j++){
				if(i>>j&1)po+=mem[j];
				else go+=mem[j];
			}
			if(abs(go-po)<min)min=abs(go-po);
		}
		cout<<min<<endl;
	}
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int a[20];
int main() {
	int n;
	while (scanf("%d", &n), n) {
		int sum = 0;
		rep(i, n) {
			scanf("%d", &a[i]);
			sum += a[i];
		}
		int Min = INT_MAX;
		rep(i, 1 << n) {
			int cnt = 0;
			rep(j, n)if ((i >> j) & 1)cnt += a[j];
			Min = min(Min, abs(cnt - (sum - cnt)));
		}
		printf("%d\n", Min);
	}
}

C++: #include <cstdio>
#include <algorithm>
#include <tr1/unordered_map>
using namespace std;

int n , v[20];

tr1::unordered_map<int,int> memo[20];

int f(int pos , int sum){
	return pos == n ? abs(sum) : memo[pos].count(sum) ? memo[pos][sum] : memo[pos][sum] = min( f(pos+1,sum+v[pos] ) , f(pos+1,abs(sum-v[pos])) );
}

int main(){
	while(scanf("%d\n",&n)&& n){
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear() ;
		printf("%d\n",f(0,0));
	}
}

C++: #include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
	int n,x[20],i,a,b;
	while(cin>>n,n)
	{
		for(a=b=i=0;i<n;++i)cin>>x[i];
		sort(x,x+n);
		for(i=n-1;i>=0;--i)(a<b?a:b)+=x[i];
		cout<<abs(a-b)<<endl;
	}
	return 0;
}