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++: #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;

static const double EPS = 1e-8;

int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		int atk[20];
		for(int i=0;i<n;i++){
			scanf("%d",atk+i);
		}

		int diff=numeric_limits<int>::max();
		for(int S=0;S<=(1<<n)-1;S++){
			int sumA=0;
			int sumB=0;
			for(int i=0;i<n;i++){
				if(S & (1<<i)) sumA+=atk[i];
				else sumB+=atk[i];
			}
			diff = min(diff,abs(sumA-sumB));
		}

		printf("%d\n",diff);
	}
}

C++: //Name: Split Up!
//Level: 3
//Category: 全探索,DP
//Note:

/*
 * 全ての選び方を試す．
 * このとき，ある選び方が2進数 pat で与えられているとき，これに対する攻撃力の合計値は
 * patのビットをどれか1つ落としたときの合計値に，落としたビットに対応する攻撃力を加えたものとなる．
 * このとき，パターンを2進数で昇順に試していけばビットを1つ落としたパターンは必ず pat より前に試しているので，
 * これらの値を記憶しておくことで簡単に計算できるようになる．
 *
 * 落とすビットはどれでもよいが，最下位ビットが pat&(-pat) で得られるのでここではこれを使っている．
 *
 * オーダーは O(2^N)．
 * 愚直に各パターンについて合計値を求めると O(N*2^N) であるから高速化されている．
 */
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int arr[20];
int pos_of[1<<20];
int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    for(int i = 0; i < 20; ++i) pos_of[1<<i] = i;
    while(true) {
        int N;
        cin >> N;
        if(!N) break;

        int sum = 0;
        for(int i = 0; i < N; ++i) {
            cin >> arr[i];
            sum += arr[i];
        }
        int ans = 100000000;
        vector<int> memo(1<<N, 0);
        for(int pat = 1; pat < (1<<N); ++pat) {
            const int prev = pat&~(pat&-pat);
            const int suma = memo[prev] + arr[pos_of[pat&-pat]];
            const int sumb = sum - suma;
            ans = min(ans, abs(suma-sumb));
            memo[pat] = suma;
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
#include <cstring>
#include <climits>
#include <queue>
#include <map>
#include <sstream>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-9

int main(){
	int n;
	int data[21];
	while(cin>>n,n){
		int ret = INT_MAX;
		rep(i,n)cin>>data[i];
		rep(i,(1<<n)/2+1){
			int a=0,b=0;
			for(int j=0; j<n; j++){
				if( i&(1<<j) ){
					a += data[j];
				}else{
					b += data[j];
				}
			}
			ret = min(ret, abs(a-b));
		}
		printf("%d\n",ret);
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#define abs(a) ((a)>0 ? (a) : -(a))

using namespace std;

long diffmin,n,p[20];
void dfs(int layer, long a, long b){
	if (layer < n){
		dfs(layer + 1, a + p[layer], b);
		dfs(layer + 1, a, b + p[layer]);
	} else {
		if (diffmin > abs(a - b)){
			diffmin = abs(a - b);
		}
		return;
	}
}
int main(){
	long all = 0, party0 = 0, party1 = 0;
	cin >> n;
	while (n){
		for (int i = 0; i < n; i++){
			long input;
			cin >> input;
			p[i] = input;
			all += input;
		}
		diffmin = all;
		dfs(0, 0, 0);
		cout << diffmin;
		cin >> n;
		party0 = party1 = 0;
		all = 0;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
void check(int x);
int a[20];
int b,c;
int n;
int least = 1000000;
int main(){

    while(cin>>n,n){
        least = 1000000;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        check(0);
        cout<<least<<endl;
    }

    return 0;
}
void check(int x){
    if(x==n){
        int an = b - c;
        if(an<0)an*=-1;
        if(an<least)least = an;
        return;
    }
    b+=a[x];
    check(x+1);
    b-=a[x];
    c+=a[x];
    check(x+1);
    c-=a[x];

}

C++: #include<iostream>
#include<climits>
#include<cstdlib>
using namespace std;
int main()
{
  int n,a[20],sum,min,i,j;
  while(cin>>n,n){
    sum=0;
    for(i=0;i<n;i++){
      cin>>a[i];
      sum+=a[i];
    }
    min=INT_MAX;
    for(i=(1<<n)-1;i>=0;i--){
      int s=0;
      for(j=0;j<n;j++)if(i&(1<<j))s+=a[j];
      if(min>abs(sum-s-s))min=abs(sum-s-s);
    }
    cout<<min<<endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n,p[20],s[1<<20];

int main(){
	while(cin>>n){
		if(!n)return 0;
		fill(s,s+(1<<n),0);
		int sum=0;
		for(int i=0;i<n;i++){cin>>p[i];sum+=p[i];}
		for(int i=0;i<1<<n;i++){
			for(int j=0;j<n;j++)if((i/(1<<j))%2)s[i]+=p[j];
		}
		sort(s,s+(1<<n));
		int dif=1e+9;
		for(int i=0;i<1<<n;i++)dif=min(dif,abs(s[i]*2-sum));
		cout<<dif<<endl;
	}
}