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<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#include<bitset>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
/*string st(int bina,int n){
  string out;
  for (int i = 0; bina>0 ; i++){
    out = (char)((bina%2)+'0')+out;
    bina = bina/2;
  }
  while(out.size()!=n)out='0'+out;
  return out;
}  */
int main(){
  int n;
  while(cin>>n,n){
    int ans=9999999;
    vector<int>in(n);
    int sum2=0;
    rep(i,n){cin>>in[i];sum2+=in[i];}
    rep(i,1<<n){
      int sum=0;
      rep(j,n)if((i>>j)&1)sum+=in[j];
      if(ans>abs(2*sum-sum2))ans=abs(sum2-2*sum);
      //cout<<i<<" "<<sum<<endl;
    }
    cout<<ans<<endl;
  }

}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int n, a[100], ans = 9999999999;
void dfs(int g, int p, int now) {
	if (now == n) {
		ans = min(abs(g - p), ans);
		return;
	}
	dfs(g + a[now], p, now + 1);
	dfs(g, a[now] + p, now + 1);
	return;
}
int main() {
	while (1) {
		cin >> n;
		if (n == 0) return 0;
		ans = 9999999999;
		for (int i = 0; i < n; i++) cin >> a[i];
		dfs(0, 0, 0);
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

int main()
{
	int pn, gn, n, a[21];
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		pn = gn = 0;
		for(int i = 0; i < n; i++){
			cin >> a[i];
		}
		sort(a, a+n);
		
		for(int i = n-1; i >= 0; i--){
			if(pn < gn){
				pn += a[i];
			} else {
				gn += a[i];
			}
		}
		
		pn -= gn;
		if(pn < 0){
			pn = -pn;
		}
		cout << pn << endl;
	}
	
	return 0;
}

C++: //54
#include<iostream>
#include<cstdlib>

using namespace std;

int main(){
  for(int n;cin>>n,n;){
    int ans=1<<30;
    int a[20];
    for(int i=0;i<n;i++){
      cin>>a[i];
    }
    for(int i=0;i<1<<n;i++){
      int p[2]={};
      for(int j=0;j<n;j++){
	p[!(i&1<<j)]+=a[j];
      }
      ans=min(ans,abs(p[0]-p[1]));
    }
    cout<<ans<<endl;
  }
  return 0;
}

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){
	if(sum > 10000000) return 99999999;
	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(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 <vector>
#include <set>
#include <algorithm>

using namespace std;

set<int> party;

void DFS(int depth, int sum, vector<int>& a)
{
	if (depth == a.size())
	{
		party.insert(sum);
		return;
	}
	DFS(depth + 1, sum, a);
	DFS(depth + 1, sum + a[depth], a);
}

void solve()
{
	int n;
	while (cin >> n, n)
	{
		party.clear();
		int sum = 0;
		vector<int> a(n);
		for (int i = 0; i < n; ++i)
		{
			cin >> a[i];
			sum += a[i];
		}
		DFS(0, 0, a);
		int minV = 1e9;
		set<int>::iterator it = party.begin();
		while (it != party.end())
		{
			minV = min(minV, abs(sum - *it * 2));
			++it;
		}
		cout << minV << endl;
	}
}

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

C++: #include <iostream>
#include <cstring>
#include <climits>
#include <cstdlib>
using namespace std;

int n, sum, ans;
int t[22];
bool used[22];

void solve(int idx, int t_sum){
  if(idx == n){
    ans = min(ans, abs(t_sum - (sum - t_sum)));
    return;
  }

  solve(idx + 1, t_sum);
  solve(idx + 1, t_sum + t[idx]);
}

int main(){
  while(cin >> n, n){
    sum = 0;
    for(int i = 0; i < n; i++){
      cin >> t[i];
      sum += t[i];
    }

    ans = INT_MAX;
    memset(used, 0, sizeof(used));
    solve(0, 0);

    cout << ans << endl;
  }
}

C++: #include<iostream>

using namespace std;

int main(){

  int in;
  
  while(cin >> in){
    if(in == 0) break;

    int data[in];
    int sum[2] = {0,0};
    int n[in];

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

    for(int i = in-1; i >= 0; i--){
      if(sum[0] > sum[1]) sum[1]+=n[i];
      else sum[0]+=n[i];
    }

    sum[0] -= sum[1];
    if(sum[0] < 0) sum[0]*=-1;

    cout << sum[0] << endl;
  }
  return 0;
}

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

using namespace std;

int arr[20];
int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    while(true) {
        int N;
        cin >> N;
        if(!N) break;

        for(int i = 0; i < N; ++i) {
            cin >> arr[i];
        }
        int ans = 100000000;
        for(int pat = 0; pat < (1<<N); ++pat) {
            int suma = 0, sumb = 0;
            for(int i = 0; i < N; ++i) {
                if(pat & (1<<i)) suma += arr[i];
                else sumb += arr[i];
            }
            ans = min(ans, abs(suma-sumb));
        }
        cout << ans << endl;
    }
    return 0;
}