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<algorithm>
#include<cmath>
using namespace std;

int n, m[20];

int main() {
	while (true) {
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> m[i];
		}
		int a = 1 << n, b, c, d, minx = 1000000000;
		for (int i = 0; i < a; i++) {
			b = 0; c = 0; d = 0;
			for (int j = 1; j < a; j *= 2) {
				if ((i / j) % 2 == 0) {
					b += m[d];
				}
				else {
					c += m[d];
				}
				d++;
			}
			minx = min(minx, abs(b - c));
		}
		cout << minx << endl;
	}
	return 0;
}

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;
		sa = 1000000;
		if(n == 0) break;
		for(int i = 1;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;
	}
		int kari = joutai;
		flat(joutai+1,kon+deta[kari],pon);
		flat(joutai+1,kon,pon+deta[kari]);
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <algorithm>
#include <cassert>

using namespace std;



bool solve(){
	while(1){
		int n;
		cin>> n;
		if(!n) break;
		
		vector<int> a(n);
		int sum=0;
		for(int i=0;i<n;i++){
			cin>> a[i];
			sum += a[i];
		}
		//sum/2テ」ツ?ォテ、ツクツ?ァツ閉ェティツソツ妥」ツ??・ツ?、テ」ツつ津ィツソツ氾」ツ?療」ツ?淌」ツ??ッツスツ・テッツスツ・テッツスツ・
//		int ans = f(sum, 0, a, n);			//テ・ツ?催・ツクツーテ」ツ??」ツ?ィ1テ」ツ?、テ」ツ?療」ツ?凝ァツオツ静ヲツ楪愿、ツソツ敕ヲツ個?」ツ?ァテ」ツ?催」ツ?ェテ」ツ??」ツ?づ」ツつ暗」ツ?湘」ツ?ェテ」ツ??」ツ?凝」ツ?ェテ」ツ?づ」ツつ湘」ツ?凝」ツつ嘉」ツ?ャテ」ツ??
//		cout<< ans<< endl;

		//テ、ツスツ陛」ツ?凝ヲツ?ェテ」ツ?療」ツ??ヲツーツ療」ツ??
		sort(a.begin(), a.end());
		reverse(a.begin(), a.end());
		int h_sum = 0;
		int l_sum = 0;
		for(int i=0;i<n;i++){
			int temp = l_sum;
			temp += a[i];
			if(sum/2 < temp){
				h_sum = abs(h_sum-sum/2)<abs(temp-sum/2) ? h_sum : temp;
			}else{
				l_sum = temp;
			}
			if(h_sum < l_sum) h_sum = l_sum;
		}
		cout<< min(abs(2*(h_sum-1)-sum), abs(2*l_sum))<< endl;
	}
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	solve();

	return 0;
}

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

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vint;
static const int INF=1<<24;

template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T  &t = T()){
	v.assign(a,vector<T>(b,t));
}

//v.erase(unique(v.begin(),v.end()),v.end());

vint v;
ll ans;
ll a,b;
void foo(ll c){
	if(c==v.size()){
		ans=min(ans,abs(a-b));
		return;
	}
	a+=v[c];
	foo(c+1);
	a-=v[c];
	b+=v[c];
	foo(c+1);
	b-=v[c];
}

int main(){
	int n;
	while(cin>>n,n){
		v.clear();
		ans=INF;
		a=0;
		b=0;
		rep(i,n){
			ll t;
			cin>>t;
			v.PB(t);
		}
		foo(0);
		cout<<ans<<endl;
	}
}

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));
	}
	else 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 <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(v[i] >= 0){
				if(t > s) s += v[i];
				else t += v[i];
			}
			else{
				if(t < s) s += v[i];
				else t += v[i];
			}
		}

		//cout<<s<<" "<<t<<endl;

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