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 <cstdio>
#include <string.h>
#include <iostream>
#include <math.h>

using namespace std;

int main(void){
	int n, i, j, x, mn, d, dp[1 << 20];
	while(1){
		scanf("%d",&n);
		if(!n) break;
		memset(dp,-1,sizeof(dp));
		dp[0] = 0;
		for(i = 0;i < n;i++){
			scanf("%d",&x);
			for(j = 0;j < 1 << n;j++)
				if(dp[j] != -1) dp[j ^ (1 << i)] = dp[j] + x;
		}
		d = ~((1 << n) - 1);
		mn = 0x3FFFFFFF;
		for(i = 0;i < 1 << n;i++)
			mn = min(mn,abs(dp[i] - dp[(~i ^ d)]));
		printf("%d\n",mn);
	}
	return 0;
}

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

using namespace std;

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

		int ans=1<<30;
		int sum=accumulate(a,a+n,0);
		for(int i=0;i<(1<<n);i++){
			int psum=0;
			for(int j=0;j<n;j++){
				if(i&(1<<j))	psum+=a[j];
			}
			ans=min(ans,abs(sum-2*psum));
		}

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

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<climits>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>

using namespace std;

int main(){
    int n;
    int a[22];
    while(cin>>n,n){
        for(int i=0;i<n;i++)
            cin>>a[i];
        int l=0;
        int mi=INT_MAX;
        while(l<(2<<n)){
            int p=0,q=0;
            for(int i=0;i<n;i++){
                if((l>>i)&1)
                    p+=a[i];
                else
                    q+=a[i];
            }
            mi=min(abs(q-p),mi);
            l++;
        }
        cout<<mi<<endl;
    }
    
}

C++: #include<bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
int a;
while(cin>>a,a){
	int sum=0;
	vector<vector<int>>c(a+1);
	c[0].push_back(0);
	for(int b=0;b<a;b++){
		int e;
		scanf("%lld",&e);
		sum+=e;
		for(int d=0;d<c[b].size();d++){
			c[b+1].push_back(c[b][d]);
			c[b+1].push_back(c[b][d]+e);
		}
	}
	int MIN=LLONG_MAX/3;
	for(int x=0;x<c[a].size();x++){
		MIN=min(MIN,abs(c[a][x]-(sum-c[a][x])));
	}
	printf("%lld\n",MIN);
}
}

C++: #include<cstdio>
#include<sstream>
#include<string>

#define reps(i,f,n) for(int i = f; i < int(n); i++)
#define rep(i,n) reps(i,0,n)

using namespace std;


int main(void){
	int n;
	while(cin >> n){
		int ans = 100000000;
		int a[32]={0};
		if(!n)break;
		rep(i,n)cin >> a[i];
		
		rep(i,1<<n){
			int ga = 0,gb = 0;
			for(int j = 1,index = 0; j < 1<<n; j = j<<1,index++){
				if(i&j){
					ga+=a[index];
				}else{
					gb+=a[index];
				}
			}
			ans = min(abs(ga-gb), ans);
		}
		
		printf("%d\n", ans);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

int main(){
  int n;
  while(cin>>n,n){
    int a[20];
    for(int i=0;i<n;i++)cin>>a[i];
    sort(a,a+n,greater<int>());
    int A=0,B=0;
    for(int i=0;i<n;i++){
      if(A<B)A+=a[i];
      else B+=a[i];
    }
    cout <<abs(A-B)<<endl;
  }
  return 0;
}

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

using namespace std;

const int INF = 1e+9;

int n, a[21], ans;

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);
    }
}

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

        ans = INF;

        dfs(0, 0);

        cout << ans << endl;
    }
}

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

using namespace std;

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

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100000
using namespace std;

int main()
{
  while(1)
    {
      int n,in,A,B,C[N];
      cin >> n;
      if(n == 0)break;
      for(int i=0;i<n;i++)
	{
	  cin >> C[i];
	}
      sort(C,C+n);
      //for(int i=0;i<n;i++)cout << C[i] << " ";
      //cout << endl;
      A=C[n-1];     
      B=C[n-2];

      //cout << A << " " << B << endl;
      for(int i=n-3;i>=0;i--)
	{
	  if(A>B)
	    {
	      B+=C[i];
	    }
	  else if(B>A)
	    {
	      A+=C[i];
	    }
	  else 
	    {
	      A+=C[i];
	    }
	}
      int ans;
      ans = max(A,B) - min(A,B);
      cout << ans << endl;

    }



}

C++: #include<stdio.h>

int a[20];

int main(){
  int n;
  for(;scanf("%d",&n)&&n;){
    for(int i=0;i<n;++i)scanf("%d",a+i);
    int mi=1e9;
    for(int b=0;b<1<<n;++b){
      int d=0;
      for(int i=0;i<n;++i)
        d+=b&1<<i?a[i]:-a[i];
      if(d<0)d=-d;
      if(mi>d)mi=d;
    }
    printf("%d\n",mi);
  }
}