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 <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    while (cin >> N, N) {
        vector<int> v(N);
        int ans = 1 << 28;
        for (auto& i : v) cin >> i;
        for (int i = 1; i < (1<<N)-1; i++) {
            int A = 0, B = 0;
            for (int j = 0; j < N; j++)
                if (i&(1<<j)) A+=v[j];
                else B+=v[j];
            ans = min(ans, abs(A-B));
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#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( ( sum >= dp2[pos] && ans <= 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<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 999999
#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<k;i++)
typedef long long ll;
typedef unsigned long long ull;
int sum;
int mem[32];
int rec(int num,int A,int B){
    if(A + B == sum){
        int mi = min(mi,abs(A-B));
        return mi;
    }
    rec(num+1,A+mem[num],B);
    rec(num+1,A,B+mem[num]);
}
int main(){
    int n;
    while(scanf("%d",&n), n){
        sum = 0;
        memset(mem,0,sizeof(mem));
        rep(i,n){
            scanf("%d",&mem[i]);
            sum += mem[i];
        }
        printf("%d\n",rec(0,0,0));
    }
	return 0;
}

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

using namespace std;

int main(){
  int i, n, x, a, b, ans;
  vector<int> data;

  while(1){
    cin >> n;
    if(n == 0) break;

    for(i=0; i<n; ++i){
      cin >> x;
      data.push_back(x);
    }

    sort(data.begin(), data.end());

    for(i=n-1, a=0, b=0; i>=0; --i){
      if(a < b) a += data[i];
      else b += data[i];
    }

    if(a >= b) ans = a-b;
    else ans = b-a;

    cout << ans << endl;
    data.clear();
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int n; long long a[22];
vector<long long>v;
void f(int i, int sum) {
	if (i == n)v.push_back(sum);
	for (int p = 0; p <= 1; p++) {
		f(i + 1, sum + a[i] * p);
	}
	return;
}
int main() {
	long long sum = 0;
	cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; }
	f(0, 0);
	long long ans = 114514191810;
	for (int i = 0; i < v.size(); i++) {
		ans = min(ans, abs((sum - v[i]) - v[i]));
	}
	cout << ans << endl;
}

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

int power[20];

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

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

    int ans = 1<<30,tmp;
    for(int i=0; i < 1<<n; ++i) {
      tmp = 0;
      for(int j=0; j<n; ++j)
	if(i & 1<<j) tmp += power[j];
      ans = min(ans, abs(sum-tmp-tmp));
    }
    cout<<ans<<endl;
  }
}

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

vector <int> new_members;
int kotae;
int a;
int b;

int score( int a, int b )
{
	if ( a > b )
		return a - b;
	else
		return b - a;
}

void solve( int n )
{
	if ( n == new_members.size() )
	{
		kotae = min( kotae, score( a, b ) );
		return;
	}

	a += new_members[n];
	solve( n + 1 );
	a -= new_members[n];

	b += new_members[n];
	solve( n + 1 );
	b -= new_members[n];

}

int main( void )
{
	int n;
	while ( cin >> n && n )
	{
		int a = 0;
		int b = 0;

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

		kotae = INT_MAX;
		a = 0;
		b = 0;
		solve( 0 );

		cout << kotae << endl;

	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
typedef pair<int, int> P;
typedef pair<string, int> psi;

signed main(){
    int n;
    while(cin >> n, n){
        vector<int> d(n);
        int sum = 0;
        rep(i, 0, n){
            cin >> d[i];
            sum += d[i];
        }
        int ans = 1e9;
        for(int i = 0; i < 1<<n; i++){
            int tmp = 0;
            rep(j, 0, n){
                if(i & 1<<j) tmp += d[j];
            }
            ans = min(ans, abs(sum - 2*tmp));
        }
        cout << ans << endl;
    }
}