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 <limits>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	for(int n; cin >> n && n;) {
		vector<int> powers(n);
		for(auto &e : powers) cin >> e;

		const int total = accumulate(powers.begin(), powers.end(), 0);
		unordered_set<int> s{0};
		for(const auto &p : powers) {
			unordered_set<int> tmp(s);
			for(const auto &sum : tmp) {
				s.insert(sum + p);
			}
		}

		int ans = numeric_limits<int>::max();
		for(const auto &sum : s) {
			ans = min(ans, abs(sum - (total - sum)));
		}

		cout << ans << endl;
	}

	return EXIT_SUCCESS;
}

C++: #include <cstdio>
#include <string.h>
#include <iostream>

using namespace std;

int abs(int m){
	if(m > 0)
		return m;
	else
		return -m;
}

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 <iostream>
#include <vector>
#include <climits>
#include <cmath>
//#include <fstream>	// require freopen

using namespace std;

int main()
{
//	cut here before submit 
//	freopen ("testcase.splitup", "r", stdin );
	int n;

	while (cin >> n && n ){
		vector <int> party(n);
		for (int i = 0; i < n; ++i ){
			cin >> party[i];
		} // end for

		int termA = 0, termB = 0; 
		int res = INT_MAX;

		termA = party[0];

		if (n == 1 ){
			return termA;
		}else if (n == 2 ){
			termB = party[1];
			res = abs (termA - termB );
			cout << res << endl;
		}else{ // end if

			for (int i = 0; i < (1 << n); ++i ){
				termA = 0, termB = 0;
				for (int j = 0; j < n; ++j ){
					if (i & (1 << j ) ){
						termA += party[j];
					}else{
						termB += party[j];
					} // end if
				} // end for
				int diff = abs (termA - termB );
				res = min (res, diff );
			} // end for
			cout << res << endl;
		} // end if
	} // end loop
		
	return 0;
}

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

int main(){
  int n;
  while(cin >> n, n){
    vector<int> V(n);
    for(int i = 0 ; i < n ; i++) cin >> V[i];
    
    int ans = 1e9;
    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 += V[j];
	else B += V[j];
      }
      ans = min(ans, abs(A-B));
    }
    cout << ans << endl;
  }  
  return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int solve(vector<int>& a)
{
    int n = a.size();
    set<int> si;
    si.insert(0);

    for(int i=0; i<n; ++i){
        set<int> si1;
        while(!si.empty()){
            int tmp = *(si.begin());
            si.erase(si.begin());
            si1.insert(tmp + a[i]);
            si1.insert(abs(tmp - a[i]));
        }
        si = si1;
    }
    return *(si.begin());
}

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        vector<int> a(n);
        for(int i=0; i<n; ++i)
            cin >> a[i];
        cout << solve(a) << endl;
    }
}

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

using namespace std;

int main () {


	int ab[20];
	bool used[20];
	int sum;
	int aSum;
	int bSum;
	bool aUsed[20];
	bool bUsod[20];

	while (true) {
	
		int n;
		cin >> n;
		if ( n == 0)
			break;
		sum = 0;
		aSum = 0;
		bSum = 0;
		for (int i=0; i<n; i++ ) {
			cin >> ab[i];
			used[i] = false;
			aUsed[i] = false;
			bUsed[i] = false;
			sum += ab[i];
		}

		sort ( ab, &ab[n]);

		for ( int i=n; i>=0; i-- ) {
			if ( aSum <= bSum ) {
				aSum += ab[i];
				aUsed[i] = true;
			} else {
				bSum += ab[i];
				bUsed[i] = true;
			}
		}
		/*
		bool stop = true;
		while (true) {
			if (abs (aSum - bSum) / 2 == 0 ) {
				break;
			}

			for ( int i=0; i<n; i++ ) {
				if ( abs(aSum - bSum)/2 >= ab[i] ) {
					if ( aUsed > bUsed ) {

			}
		}
				*/

		cout << abs(aSum - bSum);
	}


}