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>
using namespace std;

int a[20], n, res;
const int INF = 1000001;

int abs(int va) {
    return va < 0 ? -va : va;
}

void dfs(int i, int a_sum, int b_sum) {
    if (i==n) {
        res = min(abs(a_sum - b_sum), res);
    } else {
        dfs(i+1, a_sum+a[i], b_sum);
        dfs(i+1, a_sum, b_sum+a[i]);
    }
}

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

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main( void )
{
	int n;
	while ( cin >> n && n )
	{
		int a = 0;
		int b = 0;

		vector <int> new_members;
		for ( int i = 0; i < n; i++ )
		{
			int new_member;
			cin >> new_member;
			new_members.push_back( new_member );
		}

		sort( new_members.rbegin(), new_members.rend() );

		for ( vector <int>::iterator it = new_members.begin(); it != new_members.end(); it++ )
		{
			if ( a <= b )
				a += *it;
			else
				b += *it;
		}

		if ( a > b )
			cout << a - b << endl;
		else
			cout << b - a << endl;

	}

	return 0;
}

C++: #include <iostream>
#include <queue>
#define abs(a) ((a)>0 ? (a) : -(a))

using namespace std;

long diffmin,n,p[20];
void dfs(int layer, long a, long b){
	if (layer < n){
		dfs(layer + 1, a + p[layer], b);
		dfs(layer + 1, a, b + p[layer]);
	} else {
		if (diffmin > abs(a - b)){
			diffmin = abs(a - b);
		}
		return;
	}
}
int main(){
	long all = 0, party0 = 0, party1 = 0;
	cin >> n;
	while (n){
		for (int i = 0; i < n; i++){
			long input;
			cin >> input;
			p[i] = input;
			all += input;
		}
		diffmin = all;
		dfs(0, 0, 0);
		cout << diffmin << endl;
		cin >> n;
		party0 = party1 = 0;
		all = 0;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdlib>
main(){for(int n,a[20],S,s,m,i,j,t;std::cin>>n,n;printf("%d\n",m)){for(i=S=0;i<n;S+=a[i++])std::cin>>a[i];m=1<<30;for(i=(1<<n);i-->0;t=abs(S-s-s),m>t&&(m=t))for(j=s=0;j<n;j++)if(i&1<<j)s+=a[j];}}

C++: #include<iostream>
#include<stdlib.h>
#include<limits.h>
int main()
{
	int n,x[20],i,j,a,b,r,t;
	struct S{int a,b;};
	static S s[2097152];
	while(std::cin>>n,n)
	{
		for(a=b=i=0;i<n;++i)std::cin>>x[i];
		s[0].a=s[0].b=0;
		for(i=0;i<n;++i)for(j=(1<<i)-1;j<(1<<(i+1))-1;++j)
		{
			s[j*2+1]=s[j],s[j*2+1].a+=x[i];
			s[j*2+2]=s[j],s[j*2+2].b+=x[i];
		}
		for(r=INT_MAX,i=(1<<n)-1;i<(1<<(n+1))-1;++i)
			t=abs(s[i].a-s[i].b),r=(r<t?r:t);
		std::cout<<r<<'\n';
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int n;
int dfs(int i, vector<int> a, int sum, int max){
  if(i==n) return abs(max-sum*2);
  if((sum+a[i])*2>max) return dfs(i+1,a,sum,max);
  return min( dfs(i+1,a,sum+a[i],max), dfs(i+1,a,sum,max));
}
int main(void){
  while(cin>>n){
    if(n==0) break;
    vector<int> a(n);
    REP(i,n) cin>>a[i];
    int s = accumulate(a.begin(),a.end(),0);
    cout<<dfs(0,a,0,s)<<endl;
  }
  return 0;
}

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

int n, ans;
int t[22];

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

    ans = INT_MAX;

    for(int i = 0; i < (1 << n); i++){
      int sumA = 0, sumB = 0;

      for(int j = 0; j < n; j++){
        if(i & (1 << j)){
          sumA += t[j];
        }
        else{
          sumB += t[j];
        }
      }

      ans = min(ans, abs(sumA - sumB));
    }

    cout << ans << endl;
  }
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int list[20];
int func(int n,int a,int b){
	if(n == -1){
		return (a>b)?(a-b):(b-a);
	}
	return min(func(n-1,a+list[n],b),func(n-1,a,b+list[n]));
}
int main(){
	int n;
	while(cin >> n,n){
		memset(list,0,sizeof(list));
		for(int i = 0; i < n; i++){
			scanf("%d",&list[i]);
		}
		int ans = func(n,0,0);
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include<cmath>
#include<cstdio>
#include<numeric>

using namespace std;

int main(){
	for(int n;scanf("%d",&n),n;){
		int a[20];
		for(int i=0;i<n;i++)	scanf("%d",a+i);

		int ans=1<<30;
		int sum=accumulate(a,a+n,0);
		for(int i=0;i<(1<<n);i++){
			int psum=0;
			for(int j=0;j<n;j++){
				if(i&(1<<j))	psum+=a[j];
			}
			ans=min(ans,abs(sum-2*psum));
		}

		printf("%d\n",ans);
	}

	return 0;
}

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

const long long int INF = 1000000000; //KÈå«¢
long long int n, ans, a[21];

void dfs(int i, long long int A, long long int B){
	if( i == n ){
		ans = min( ans , abs(A-B) );
		return;
	}
	dfs( i+1 , A+a[i] , B );
	dfs( i+1 , A , B+a[i] );
	
}

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