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<bits/stdc++.h>
using namespace std;
#define INF (1<<22)

int n;
int a[22];
int ans;

void solve(int now,int idx){
  if(idx == n){
    ans = min(ans,abs(now));
    return;
  }
  solve(now-a[idx],idx+1);
  solve(now+a[idx],idx+1);
}

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

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

using namespace std;

int main(){

  int in;
  
  while(cin >> in){
    if(in == 0) break;

    int data[in];
    int sum[2] = {0,0};
    int n[in];

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

    for(int i = in-1; i >= 0; i--){
      if(sum[0] > sum[1]) sum[1]+=n[i];
      else sum[0]+=n[i];
    }

    sum[0] -= sum[1];
    if(sum[0] < 0) sum[0]*=-1;

    cout << sum[0] << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<complex>
#include<math.h>
using namespace std;
int main(){
	int n,k[20];
	while(true){
		cin>>n;
		if(n==0)
			break;
		int sum=0;
		for(int i=0;i<n;i++){
			cin>>k[i];
			sum+=k[i];
			k[i]*=2;
		}
		int best=1000000;
		int m=pow(2.0,n+0.0);
		for(int i=0;i<m;i++){
			int bit=i;
			int count=0;
			for(int j=0;j<n;j++){
				if(((bit>>(j+1))&1)==1)count+=k[j];}
			best=min(best,abs(count-sum));
		}
		cout<<best<<endl;
	}
	return 0;
}

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

const int INF = 1e+9; // 10^9 を表す.
int n, a[21], ans;

// diff は パーティAとBの戦闘力の差
void dfs(int diff, int k){
    if( k == n ){ //  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<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

int data[99];

int solve(int n, int a, int b){

  if(!n) return abs(a-b);

  return min(solve(n-1, a+data[n], b), solve(n-1, a, b+data[n]));
}

int main(){

  int n;
  while(cin >> n, n){
    for(int i=1;i<=n;i++) cin >> data[i];
    cout << solve(n, 0, 0) << endl;
  }

}

C++: #include<iostream>
#include<vector>
#include<string>
#include<cstdlib>
#include<sstream>
#include<queue>
#include<deque>
#include<cmath>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<iomanip>
#define INF 2147483647
#define lli long long int
#define MOD 1000000007

using namespace std;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;

int dfs(int depth,vi a,int ponta,int gonta){
	if (depth == a.size()){
		return abs(ponta - gonta);	
	}

	return min(dfs(depth + 1,a,ponta + a[depth],gonta),dfs(depth + 1,a,ponta,gonta + a[depth]) );
}

int main() {

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

		vi a(n);
		for (int i = 0; i < n; i++){
			cin >> a[i];
		}
		int ponta = 0;
		int gonta = 0;
		cout << dfs(0,a,ponta,gonta) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;

int main(){
	
	int n;
	while(cin>>n,n!=0){
	vector<int> a(n);
	
	for(int i=0;i<n;i++)
		cin>>a[i];
	
	sort(a.begin(),a.end(),greater<int>());
	
	
	for(int i=0;i<n-1;i++){
		a[i+1]=a[i]-a[i+1];
		a[i]=1000000;
		sort(a.begin(),a.end(),greater<int>());
	}
	cout<<a[n-1]<<endl;
	}
}

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

int main() {
  int n;
  while(cin >>n, n) {
    vector<int> v;
    int x;
    for(int i=0; i<n; i++) {
      cin >> x;
      v.push_back(x);
    }
    sort(v.begin(),v.end(), greater<int>());

    int a=0, b=0;
    for(int i=0; i<v.size(); i++) {
      if (a>b)
        b += v[i];
      else
        a += v[i];
    }
    cout << abs(b-a) << endl;
  }
}

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.emplace(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;
}