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 <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e15+9
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

ll n, table[100];

ll ans;

void solve(int num, int asum, int bsum){
	if (num == n){
		ans = min(ans, (ll)(abs(asum - bsum)));
		return;
	}

	solve(num + 1, asum + table[num], bsum);
	solve(num + 1, asum, bsum + table[num]);
}

int main(){
	while (cin >> n&&n){
		ans = INF;
		REP(i, n){
			cin >> table[i];
		}
		solve(0, 0, 0);
		cout << ans << endl;
	}
}

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

int main(){
  int n, a[100], suma=0, sumb=0;

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

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

    sort(a, a+n, std::greater<int>());

    for(int i = 0;i<n;i++){
      if(suma <= sumb){
        suma += a[i];
      }else{
        sumb += a[i];
      }
    }

    cout << abs(suma - sumb) << endl;

    suma = 0;
    sumb = 0;
  }

  return 0;
}

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

int main(){
  int n;
  int a[30];

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

    int ans = 30000000;
    for(int i=0;i<(1<<n);i++){
      int A=0,B=0;
      for(int j=0;j<n;j++){
	if((i>>j)&1)A += a[j];
	else B += a[j];
      }
      ans = min(ans,abs(A-B));
    }
    cout << ans << endl;
  }
}

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

using namespace std;

int main()
{
	int n;
	
	for(;;)
	{
		vector<int> num;
		cin >> n;
		if(n==0)
			break;
		for(int i=0;i<n;i++)
		{
			int a;
			cin >> a;
			num.push_back(a);
		}
		
		sort(num.begin(),num.end());
		
		int a=0,b=0;
		int a_f=1;
		
		if(num.size()==2)
		{
			cout << num[1]-num[0] << endl;
		}
		else if(num.size()==3)
		{
			cout << num[2]-num[1]-num[0] << endl;
		}
		else
		{
			if(num.size()%2==0)
			{
				for(int i=0,j=num.size()-1;i<j;i++,j--)
				{
					if(a_f==1)
					{
						a+=num[i]+num[j];
						a_f=0;
					}
					else
					{
						b+=num[i]+num[j];
						a_f=1;
					}
				}
			}
			else
			{
				for(int i=1,j=num.size()-1;i<j;i++,j--)
				{
					if(a_f==1)
					{
						a+=num[i]+num[j];
						a_f=0;
					}
					else
					{
						b+=num[i]+num[j];
						a_f=1;
					}
				}
				if(a<b)
					a+=num[0];
				else
					b+=num[0];
			}
			
			int ans_a,ans_b;
			ans_a=max(a,b);
			ans_b=min(a,b);
//			cout << "ans_a:" << ans_a << " ans_b:" << ans_b << endl;
			cout << ans_a-ans_b << endl;
		}
	}
}

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

using namespace std;

const int INF=1e+9;
int n,ans;
int a[21];


void dfs(int diff,int k);

int main(){
	
	while(cin>>n&&n!=0){

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

void dfs(int diff,int k){
	if(k==n){
		ans=min(ans,abs(diff));
	}else{
		dfs(diff+a[k],k+1);
		dfs(diff-a[k],k+1);
	}
}

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

using namespace std;

int main(){

	int n;
	while(cin>>n&&n!=0){
		int a[20];
		int sum=0;
		for(int i=0; i < n; i++){
			cin >> a[i];
			sum+=a[i];
		}
		sort(a,a+n);

		int Asum=0;
		vector<int>A,B;
		for(int i = 0; i < n; i++){
			if(Asum < sum){
				A.push_back(a[i]);
				Asum+=a[i];
				sum-=a[i];
			}
			else{
				for(int j = i; j < n; j++){
					B.push_back(a[j]);
				}
				break;
			}
		}

		int minSum = abs(Asum-sum);
		for(int i = 0; i < A.size(); i++){
			if(Asum <= sum)
				break;
			Asum-=a[i];
			sum+=a[i];
			minSum = min(minSum,abs(Asum-sum));
		}
		cout << minSum << endl;
	}

	return 0;
}

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

#define MAX_N 20

int n;
int power[MAX_N];

int min_diff(int a, int b, int p) {
    if (p == n) return abs(a-b);
    return min(min_diff(a+power[p], b, p+1), min_diff(a, b+power[p], p+1));
}

int main() {
    while (cin >> n) {
        if (n == 0) break;
        for (int i = 0; i < n; i++) {
            cin >> power[i];
        }
        cout << min_diff(0, 0, 0) << endl;
    }
    return 0;
}