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 <stdio.h>

int min(int,int);
int getPowerDiff(int,int,int,int[20]);

int main(){
  int n,i,a[20];
  while(1){
    scanf("%d",&n);
    if(n == 0){ break; }
    for(i = 0 ; i < n ; i++){
      scanf("%d",&a[i]);
    }
    printf("%d\n",getPowerDiff(0,0,n-1,a));
  }
  return 0;
}


/*
  aテ」ツ?ィbテ」ツ?ョテヲツ慊?・ツーツ湘・ツ?、テ」ツつ津ィツソツ氾」ツ??
 */
int min(int a,int b){
  if(a < b){
    return a;
  }else{
    return b;
  }
}

/*
  aテ」ツ?ィbテ」ツ?ョテ・ツキツョテ」ツ?ョテァツオツカテ・ツッツセテ・ツ?、テ」ツつ津ィツソツ氾」ツ??
 */
int myAbs(int a,int b){
  if(a < b){
    return b - a;
  }else{
    return a - b;
  }
}

/*
  Aテ」ツ?ッテ」ツつーテ」ツδォテ」ツδシテ」ツδ輸テ」ツ?ョテヲツ按ヲテゥツ猟佚・ツ環崚」ツ?ョテ・ツ青暗ィツィツ暗・ツ?、.
  Bテ」ツ?ッテ」ツつーテ」ツδォテ」ツδシテ」ツδ唯テ」ツ?ョテヲツ按ヲテゥツ猟佚・ツ環崚」ツ?ョテ・ツ青暗ィツィツ暗・ツ?、.
  iテ」ツ?ッテ、ツサツ甘」ツ?ゥテ」ツ?ョテァツ卍サテゥツ個イティツ??」ツつ津・ツッツセティツアツ。テ」ツ?ィテ」ツ?療」ツ?ヲテ」ツ??」ツつ凝」ツ??
 */
int getPowerDiff(int A,int B,int i,int a[20]){
  /*
    i == -1テ」ツ?ィテ」ツ?ェテ」ツつ凝」ツ?ィテ」ツ??
    テ・ツ?ィテ」ツ?ヲテ」ツ?ョテァツ卍サテゥツ個イティツ??」ツつ津」ツつーテ」ツδォテ」ツδシテ」ツδ輸テ」ツ?セテ」ツ?淌」ツ?ッBテ」ツ?ォテ・ツ按?」ツ?妥」ツ?淌」ツ?ョテ」ツ?ァ
    テ」ツ?敕」ツ?ョテ」ツ?ィテ」ツ?催」ツ?ョテヲツ按ヲテゥツ猟佚・ツ環崚」ツ?ョテ・ツキツョテ」ツ?ョテァツオツカテ・ツッツセテ・ツ?、テ」ツつ津ィツソツ氾」ツ??
   */
  if(i == -1){
    return myAbs(A,B);
  }
  
  /*
    iテァツ閉ェテァツ崢ョテ」ツ?ョテァツ卍サテゥツ個イティツ??」ツつ津」ツつーテ」ツδォテ」ツδシテ」ツδ輸テ」ツ?ォテ・ツ環?」ツ?暗」ツつ凝」ツ?凝」ツ??」ツつーテ」ツδォテ」ツδシテ」ツδ唯テ」ツ?ォテ・ツ環?」ツ?暗」ツつ凝」ツ??
   */
  return min(getPowerDiff(A+a[i],B,i-1,a),
             getPowerDiff(A,B+a[i],i-1,a));
}

C++: #include<iostream>
#include<cmath>
using namespace std;
#define INF = (1 << 30)
#define rep(i,n) for(int i = 0 ; i < (n) ; ++i)
int n,ANS,vc[20];

void dfs(int rec,int now){
  if(now == n){
    ANS = min(ANS,abs(rec));
    return;
  }
  dfs(rec+vc[now],now+1);
  dfs(rec-vc[now],now+1);
}

int main(){
  while(cin >> n , n){
    ANS = INF;
    rep(i,n) cin >> vc[i];
    dfs(0,0);
    cout << ANS << endl;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int n;
int a[21];

int f(int i,int sum1,int sum2){
	if(i==n)return abs(sum1-sum2);
	return min(f(i+1,sum1+a[i],sum2),f(i+1,sum1,sum2+a[i]));
}

int main(){
	while(cin>>n,n){
		rep(i,n)cin>>a[i];
		cout<<f(0,0,0)<<endl;
	}
	return 0;
}

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

#define INF 1000000000
int n,a[21],ans;

void dfs(int diff,int k){
  if (k==n)
    ans=min(ans,abs(diff));
  else{
    dfs(diff+a[k],k+1);
    dfs(diff-a[k],k+1);
  }
}

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

C++: #include <iostream>
#include <cmath>
#define MIN(a,b) ((a>b)?(b):(a))
using namespace std;
int m=9999999;
int a[20];
	int n;
int solve(int i,int x,int y){
	if(i==n){
		m=MIN(abs(x-y),m);
		return 0;
	}
	solve(i+1,x,y+a[i]);
	solve(i+1,x+a[i],y);
	return 0;
}

int main(void){
	while(cin>>n,n){
		m=9999999;
		for(int i=0;i<n;i++)cin>>a[i];
		solve(0,0,0);
		cout<<m<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
#include<unordered_map>
using namespace std;
typedef long long ll;
ll c[20], n;
ll solve(int i, ll a, ll b) {
	if (i == n) {
		ll d = a - b;
		if (d < 0) d = 0 - d;
		return d;
	}
	ll sum = 900000000000000000;
	sum = min(sum, solve(i + 1, a + c[i], b));
	sum = min(sum, solve(i + 1, a, b + c[i]));
	return sum;
}
int main() {
	while (cin >> n&&n != 0) {
		for (int i = 0; i < n; i++)
			cin >> c[i];
		cout << solve(0, 0, 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){
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear() ;
		ans = 99999999;
		top = 0;
		X[top] = 0;
		Y[top] = 0;
		while(~top){
			int pos = X[top] , sum = Y[top]; top--;
			if(memo[pos].count(sum)) continue;
			else memo[pos].insert(sum);
			if(pos==n) ans = min(ans,sum);
			else{
				X[++top] = pos+1;
				Y[top]   = sum+v[pos];
				X[++top] = pos+1;
				Y[top]   = abs(sum-v[pos]);
			}
		}
		printf("%d\n",ans);
	}
}