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 <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
 
const int INF = 1000000;


int main(){
	int n;
	int s = 0, t = 0;
	while(cin >> n && n > 0){
		s = 0, t = 0;
		vi v(n, 0);
		rep(i, n) cin>>v[i];
		sort(all(v));
		reverse(all(v));
		rep(i, n){
			if(t > s) s += v[i];
			else t += v[i];
		}

		cout<<abs(s - t)<<endl;
	}
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <complex>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		int sum = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
		}
		int res = 1000000000;
		rep(i, (1<<n)) {
			int A = 0;
			rep(j, n) if (i & (1 << j)) A += a[j];
			if (res > abs(sum - 2 * A)) res = abs(sum - 2 * A);
			//res = min(res, abs(A - (sum - A)));
		}
		cout << res << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<map>

using namespace std;

int n;
int *sa, *sb, *sc, *a;

int bit(int m) {
	int ans = 1;
	for (int i = 0; i < m; i++)
		ans *= 2;
	return ans;
}

int main() {
	while (1) {
		cin >> n;
		if (n == 0)
			break;
		sa = new int[bit(n)];
		sb = new int[bit(n)];
		sc = new int[bit(n)];
		a = new int[n];
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < bit(n); i++) {
			for (int j = 0; j < n; j++) {
				if (i >> j & 1)
					sa[i] += a[j];
				else
					sb[i] += a[j];
			}
			sc[i] = abs(sa[i] - sb[i]);
		}
		int min = 1000000;
		for (int i = 0; i < bit(n); i++) {
			if (min > sc[i])
				min = sc[i];
		}
		cout << min << endl;


	}


	return 0;
}

C++: #include <iostream>
#define INF (1<<21)
using namespace std;

int n,t[20],mi,a[20];

void rec(int p){
  int a1,a2;
  if(p == n){
    a1 = 0;
    a2 = 0;
    for(int i=0;i<n;i++){
      if(t[i] == 1) a1 += a[i];
      else a2 += a[i];
    }
    if(a1 - a2 >= 0 && a1 - a2 < mi) mi = a1 - a2;
    else if(a2 - a1 > 0 && a2 - a1 < mi) mi = a2 - a1;
    return;
  }

  for(int k=1;k<=2;k++){
  t[p] = k;
  rec(p+1);
  }
}

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

    mi = INF;
    rec(0);
    cout << mi << endl;

  }
  return 0;
}

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

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

using namespace std;

int sum( vector<int> v ) {
  int sum = 0;
  for ( const int val : v ) {
	sum += val;
  }
  return sum;
}

int diff( int i, vector<int>& v ) {
  int sumA = 0;
  int sumB = 0;
  for ( int index = 0; index < v.size(); index++ ) {
	if ( i % 2 == 0 ) {
	  sumA += v[ index ];
	} else {
	  sumB += v[ index ];
	}
	i /= 2;
  }
  return abs( sumA - sumB );
}

int main() {
  
  while( true ){
	int n;
	cin >> n;
	if ( n == 0 ) break;
	vector<int> v( n );
	for ( int i = 0; i < n; i++ ) {
	  cin >> v[ i ];
	}
	int ans = sum( v );
	for ( int i = 0; i < (1 << n); i++ ) {
	  ans = min( ans, diff( i, v ) );
	}
	cout << ans << endl;
  }  
}