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 <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;

int n;
int mn=2000000000;
int a[20];

void dfs(int battle , int depth){
	if(depth==n){
		mn=min( mn , abs(battle));
	} else {
		dfs(battle+a[depth] , depth+1);
		dfs(battle-a[depth] , depth+1);
	}
}


int main(){
	while(cin>>n , n!=0){
		for(int i=0;i<n;++i){
			cin >> a[i];
		}
		dfs(0 , 0);
		cout << mn << endl;
	}
	return 0; 
}

C++: #include<iostream>
#include<math.h>

using namespace std;

int n,mem[100],sum,ans;

void search(int max,int total,int now)
{
	if(now>max)
		return ;
	
	total+=mem[now];
	search(max,total,now+1);
	total-=mem[now];
	search(max,total,now+1);
	
	if(sqrt((total*2-sum)*(total*2-sum))<ans)
		ans=sqrt((total*2-sum)*(total*2-sum));
	
	return ;
}

int main()
{
	int i,j;
	
	for(;;)
	{
		cin >> n;
		if(n==0)
			break;
		
		sum=0;
		for(i=0;i<n;i++)
		{
			cin >> mem[i];
			sum+=mem[i];
		}
		ans=sum;
		
		search(n-1,0,0);
		
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;

int main(){
	
	int n;
	while(cin>>n,n!=0){
	vector<int> a(n);
	
	for(int i=0;i<n;i++)
		cin>>a[i];
	
	sort(a.begin(),a.end(),greater<int>());
	
	
	for(int i=0;i<n-1;i++){
		a[0]=a[0]-a[1];
		a[1]=-1000000;
		sort(a.begin(),a.end(),greater<int>());
	}
	cout<<a[0]<<endl;
	}
}

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<queue>
using namespace std;
int main(){
	int n;
	priority_queue<int> que;
	while(cin>>n,n){
		int a=0,b=0,x;
		for(int i=0;i<n;i++){
			cin>>x;
			que.push(x);
		
		}
		while(!que.empty()){
		if(a>b)
		b+=que.top();
		else
		a+=que.top();
		que.pop();
		}
		cout<<abs(a-b)<<endl;
	}
	return 0;
}

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

int n;
int a[20];

int main() {
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) cin >> a[i];
		
		int ssum = 0;
		for (int i = 0; i < n; i++) ssum += a[i];
		
		int ans = 1145141919;
		for (int i = 0; i < (1 << n); i++) {
			int x = 0;
			for (int j = 0; j < n; j++) {
				if ((i >> j) % 2) {
					x += a[j];
				}
			}
			ans = min(ans, abs(ssum - 2 * x));
		}
		cout << ans << 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 dp[2][21];
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];
			dp[0][i] = dp[1][i] = 1000000000;
		}
		int k = 0;
		rep(i, n) {
			dp[k][i] = sum - 2*a[i];
			rep(j, i) {
				if (dp[1-k][j] > 0 && dp[1-k][j] > abs(dp[1-k][j]-2*a[i]))
					dp[k][j] = dp[1-k][j] - 2*a[i];
				else
					dp[k][j] = dp[1-k][j];
			}
			k = 1 - k;
		}
		int res = 1000000000;
		rep(i, n) res = min(res, abs(dp[1-k][i]));
		cout << res << endl;
	}
	return 0;
}

#if 0
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;
}
#endif