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 <vector>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <cmath>
using namespace std;

#define rep(i, n) for(int i = 0; i< n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 29;
const double EPS = 1e-10;


int main(){
  int a[20];
  int n;
  while(cin >> n && n){
    int res = INF;
    rep(i, n) cin >> a[i];
    rep(i, 1 << n){
      int dif = 0;
      rep(j, n){
	if(i & (1 << j)) dif += a[j];
	else dif -= a[j];
      }
      res = min(res, abs(dif));
    }
    cout << res << endl;
  }
  return 0;
}

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

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)

typedef vector<int> vi;

int calc(vi& a,int bit)
{
	int sum[2]={0};
	rep(i,a.size())
		sum[!!((1<<i)&bit)]+=a[i];
	return abs(sum[0]-sum[1]);
}

int main()
{
	for(int n;cin>>n,n;){
		vi a(n);
		rep(i,n)
			cin>>a[i];
		int res=20000000;
		rep(i,1<<n)
			res=min(res,calc(a,i));
		cout<<res<<endl;
    }
	
	return 0;
}

C++: #include<iostream>
#include<cmath>
#include<algorithm>
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>
using namespace std;

int n, m[20];

int main() {
	while (true) {
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> m[i];
		}
		int a = 1 << n, b, c, d, minx = 1000000000;
		for (int i = 0; i < a; i++) {
			b = 0; c = 0; d = 0;
			for (int j = 1; j < a; j *= 2) {
				if ((i / j) % 2 == 0) {
					b += m[d];
				}
				else {
					c += m[d];
				}
				d++;
			}
			minx = min(minx, abs(b - c));
		}
		cout << minx << endl;
	}
	return 0;
}

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

int main(){
  int n;
  int a[30];

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

    int ans = 30000000;
    for(int i=0;i<(1<<n);i++){
      int A=0,B=0;
      for(int j=0;j<n;j++){
	if((i>>j)&1)A += a[j];
	else B += a[j];
      }
      ans = min(ans,abs(A-B));
    }
    cout << ans << endl;
  }
}

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

int abs(int a){
  return (a>0)?a:-a;
}

int main(){
  int n,total;
  int a[20];
  set<int> prob;

  for(;;){
    cin>>n;
    if(!n) break;

    total=0;
    for(int i=0; i<n; i++)
      cin>>a[i],total+=a[i];

    prob.insert(0);

    set<int> c;
    for(int i=0;i<n;i++){
      copy(prob.begin(), prob.end(), insert_iterator<set<int> >(c, c.end()));
      for(set<int>::iterator it=c.begin();it!=c.end();++it){
	if((*it)+a[i] > total/2 + 1) continue;
	prob.insert((*it)+a[i]);
      }
    }

    cout<<abs (total - (*(--prob.end()))*2 )<<endl;

    prob.clear();
    //cout<<"total: "<<total<<endl;
  }
}

C++: #include <iostream>
#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,sum-v[pos]) );
}

int main(){
	while(cin >> n && n){
		for(int i = 0 ; i < n ; i++) cin >> v[i] , memo[i].clear() ;
		cout << f(0,0) << endl;
	}
}

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

using namespace std;

int RegNum;//登録者数
int RegATK[20];//登録者戦闘力
int RegATKAll;//登録者戦闘力合計
int PartyATK[2];//パーティー戦闘力

int GetMinATK(int n,int PAATK){
	if (n == 0)return abs(RegATKAll - (2 * PAATK));
	return min(GetMinATK(n - 1, PAATK + RegATK[n]), GetMinATK(n - 1, PAATK));
}

int main(){

	while (1){
		//変数初期化
		PartyATK[0] = 0; PartyATK[1] = 0;
		RegATKAll = 0;

		cin >> RegNum;//登録者数代入
		if (RegNum == 0)break;//0ならbreak
		for (int i = 0; i < RegNum; ++i){//戦闘力代入
			cin >> RegATK[i];
			RegATKAll += RegATK[i];
		}

		cout << GetMinATK(RegNum, 0) << endl;
	}

	return 0;
}

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];
    S::iterator it;
    mp[0].insert(0);
    for(int i=0;i<n;i++)
      for(it=mp[i].begin();it!=mp[i].end();it++)mp[i+1].insert((*it)+a[i]),mp[i+1].insert(*it);

    int ans=1e9;
    for(it=mp[n].begin();it!=mp[n].end();it++)ans=min(ans,sum-(*it));
    cout <<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int abs(int a){return a < 0 ? -a : a;}
int main(){
    int n;
    int a[22];
    while(cin>>n,n){
        for(int i=0;i<n;i++)
            cin>>a[i];
        int mi=1<<21;
        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;
    }
    
}