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 <cmath>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

vector<int> Vec;
int n;
int Ans = 1e+9;

void DFS(int a, int b, int depth)
{
	if(depth == n)
	{
		Ans = min(Ans, abs(a - b));
	}
	else
	{
		DFS(a + Vec[depth], b, depth + 1);
		DFS(a, b + Vec[depth], depth + 1);
	}
}

void solve()
{
	while(cin >> n, n)
	{
		Vec.resize(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Vec[i];
		}
		sort(Vec.begin(), Vec.end(), greater<int>());
		DFS(0, 0, 0);
		cout << Ans << endl;
	}
}

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

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 dp[2][1000001];
int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		int sum = 0, maxi = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
			maxi = max(maxi, a[i]);
		}
		int k = 0;
		dp[k][abs(sum-2*a[0])] = 1;
		REP(i, 1, n) {
			rep(j, maxi+1) if (dp[k][abs(sum-2*j)])
				dp[1-k][abs(sum-2*(j+a[i]))] = 1;
			k = 1 - k;
		}
		rep(i, maxi+1) if (dp[1-n%2][i]) {
			cout << i << endl;
			break;
		}
	}
	return 0;
}

#if 0
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];
			res = min(res, abs(A - (sum - A)));
		}
		cout << res << endl;
	}
	return 0;
}
#endif

C++: #include<cstdio>
#include<cstdlib>

int n,deta[30],kon,pon;
int sa = 1000000;
int max;
void flat(int joutai,int kon,int pon);

using namespace std;
int main(void)
{
	while(scanf("%d",&n))
	{
		max = 0;
		if(n == 0) break;
		for(int i = 0;i<n;i++)
		{
			scanf("%d",&deta[i]);
			max += deta[i];
		}
			flat(0,0,0);
			printf("%d\n",sa);
	}
	return 0;
}

void flat(int joutai,int pon,int kon)
{
	if(pon + kon == max)
	{
		if(sa > abs(pon-kon)) sa = abs(pon - kon);
		return;
	}
		flat(joutai+1,kon+deta[joutai],pon);
		flat(joutai+1,kon,pon+deta[joutai]);
}

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

int main() {
  int n;
  
  while(cin >> n, n) {
    int data[20]={}, all = 0;
    for(int i=0; i<n; i++) {
      cin >> data[i];
      all += data[i];
    }
    
    int ans = 1<<23;
    for(int i=0; i<(1<<n); i++) {
      int a = 0;
      for(int j=0; j<n; j++) {
	if((i>>j) & 1) {
	  a += data[j];
	  ans = min(ans, abs(a-(all-a)));
	}
      }
    }
    cout << ans << endl;
  }
  
  return 0;
}

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

int main() {
    int n;
    while (cin >> n, n) {
        vector<int> as(n);
        int sum = 0;
        for (int i = 0; i < n; i++) {
            cin >> as[i];
            sum += as[i];
        }
        int ans = 1 << 29;
        for (int i = 1, size = 1 << (n-1); i < size; i++) {
            int a = 0;
            for (int j = 0; j < n-1; j++) {
                if (i & (1 << j)) {
                    a += as[j];
                }
            }
            ans = min(ans, abs(sum - 2 * a));
        }
        cout << ans << endl;
    }
}

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

using namespace std;

int A[32];

int getMini(int idx, int costA, int costB)
{
	if (idx == -1) return (abs(costA - costB));
	
	return (min(getMini(idx - 1, costA + A[idx], costB),
				getMini(idx - 1, costA, costB + A[idx])));
}

int main()
{
	int n;
	
	while (scanf("%d", &n) && n){
		for (int i = 0; i < n; i++){
			scanf("%d", A + i);
		}
		
		printf("%d\n", getMini(n - 1, 0, 0));
	}
	
	return (0);
}