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<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 mi = INF;
void rec(int num,int A,int B){
    if(A + B == sum){
        mi = min(mi,abs(A-B));
        return ;
    }
    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;
        mi = INF;
        memset(mem,0,sizeof(mem));
        rep(i,n){
            scanf("%d",&mem[i]);
            sum += mem[i];
        }
        rec(0,0,0);
        printf("%d\n",mi);
    }
	return 0;
}

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

using namespace std;

int main( void )
{
	int n;
	int teamA, teamB, map[20];
	while(cin >> n, n) {
		int sum = 0, ave;
		for (int i = 0; i < n; ++i) { cin >> map[i]; sum += map[i]; }
		ave = sum / 2;
		sort(map, map+n, greater<int>());
		teamA = teamB = 0;
		for (int i = 0; i < n; ++i) {
			if(teamA + map[i] == ave)
				{ teamA += map[i]; continue; }
			else if(teamB + map[i] == ave)
				{ teamB += map[i]; continue; }
			if(teamA > teamB) teamB += map[i];
			else				teamA += map[i];
		}
		cout << abs(teamA - teamB) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;

int main(){
	
	int n;
	
	while(cin>>n,n!=0){
		int a=0,b=0;
		vector<int> s(n);
	
	for(int i=0;i<n;i++)
		cin>>s[i];
	
	sort(s.begin(),s.end(),greater<int>());
	
	
		for(int i=0;i<n;i++){
			if(a>b)
				b+=s[i];
			else
				a+=s[i];
		}
		cout<<abs(a-b)<<endl;
	}
}

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

using namespace std;

#define PONTA true
#define GONTA false

int n;
int getMin(bool* cmb, bool party, int i, int* pows);
int getDiff(bool* cmb, int* pows);

int main(){
	while(cin >> n && n){
		int pows[n];
		bool cmb[n];
		for(int i = 0; i < n; i++) cin >> pows[i];
	
		cout << min( getMin(cmb, PONTA, 0, pows), getMin(cmb, GONTA, 0, pows) ) << endl;
	}
	return 0;
}

int getMin(bool* cmb, bool party, int i, int* pows){
	cmb[i] = party;
	return (i == n-1)? getDiff(cmb, pows):
		min( getMin(cmb, PONTA, i+1, pows), getMin(cmb, GONTA, i+1, pows) );
}

int getDiff(bool* cmb, int* pows){
	int p = 0, g = 0;
	for(int i = 0; i < n; i++){
		if(cmb[i] == PONTA) p += pows[i];
		else g += pows[i];
	}
	return abs(p-g);
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <cstring>
#include <cmath>

using namespace std;


int main(int argc, char *argv[]) {
  int n;
  vector<int> member;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      member.push_back(tmp);
    }
    sort(member.begin(), member.end(), greater<int>());

    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
      if (x >= y)
        y += member[i];
      else
        x += member[i];
    }
    cout << abs(x - y) << endl;
  }
  return 0;
}

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

using namespace std;

int main(){
  int in;
  while(cin >> in && in){
    int data[in];
    for(int i = 0; i < in; i++) cin >> data[i];
    sort(data,data+in);
    reverse(data,data+in);
    int sum[2] = {0,0};
    for(int i = 0; i < in; i++){
      if(sum[0] > sum[1]) sum[1] += data[i];
      else sum[0] += data[i];
    }
    cout << abs(sum[0]-sum[1]) << endl;
  }
  return 0;
}

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

using namespace std;


int a[100];
int minimum = 100000000, n, sum;
void dfs(int now,int i){
  if(i == n){
    if(minimum > abs(sum-now*2)){
      minimum = abs(sum-now*2);
      return;
    }
  }
  dfs(now, i+1);
  dfs(now-a[i], i+1);
}
int main(){
  while(1){
    sum = 0;
    cin >> n;
    if(n == 0) break;

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

    sort(a, a+n);

    dfs(sum, 0);

    cout << minimum << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>
#define mk make_pair
using namespace std;
int a[20],n;
unordered_map<pair<pair<int,int>,int>,int >m;
int dfs(int d,int x,int y){
  if(d==n){if(y<x)swap(x,y);return y-x;}
  return min(dfs(d+1,x+a[d],y),dfs(d+1,x,y+a[d]));
}
int main(){
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>a[i];
    cout<<dfs(0,0,0)<<endl;m.clear();
  }
}