Go: package main

import "fmt"

func main() {
	ans := make([]int, 0)
	for {
		var n int
		fmt.Scan(&n)
		if n == 0 {
			break
		}
		a := make([]int, n)
		for i := range a {
			fmt.Scan(&a[i])
		}
		mind := int(1e9)
		for bits := 0; bits < (1 << uint(n)); bits++ {
			gsum, psum := 0, 0
			for j := 0; j < n; j++ {
				if (bits & (1 << uint(j))) != 0 {
					gsum += a[j]
				} else {
					psum += a[j]
				}
			}
			mind = min(mind, abs(gsum-psum))
		}
		ans = append(ans, mind)
	}
	for i := range ans {
		fmt.Println(ans[i])
	}
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int a[20];
int main() {
	int n;
	while (scanf("%d", &n), n) {
		int sum = 0;
		rep(i, n) {
			scanf("%d", &a[i]);
			sum += a[i];
		}
		int Min = INT_MAX;
		rep(i, 1 << n) {
			int cnt = 0;
			rep(j, n)if ((i >> j) & 1)cnt += a[j];
			Min = min(Min, abs(cnt - (sum - cnt)));
		}
		printf("%d\n", Min);
	}
}

C++: #include <cstdio>
#include <algorithm>
#include <tr1/unordered_map>
using namespace std;

int n , v[20];

tr1::unordered_map<int,int> memo[20];

int f(int pos , int sum){
	return pos == n ? abs(sum) : memo[pos].count(sum) ? memo[pos][sum] : memo[pos][sum] = min( f(pos+1,sum+v[pos] ) , f(pos+1,abs(sum-v[pos])) );
}

int main(){
	while(scanf("%d\n",&n)&& n){
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear() ;
		printf("%d\n",f(0,0));
	}
}

C++: #include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
	int n,x[20],i,a,b;
	while(cin>>n,n)
	{
		for(a=b=i=0;i<n;++i)cin>>x[i];
		sort(x,x+n);
		for(i=n-1;i>=0;--i)(a<b?a:b)+=x[i];
		cout<<abs(a-b)<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (int)(a); v < (int)(b); ++v)
#define FORE(v, a, b) for(int v = (int)(a); v <= (int)(b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define RS resize
#define CLR clear
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
using namespace std;

void split(string str, char chr, vector< string > *res){
	stringstream ss(str);
	string itm;
	while(getline(ss, itm, chr)){
		(*res).PB(itm);
	}
}

int n;
vector< int > a;

int rec(int x, int y, int i){
	if(i == n) return abs(x - y);
	
	return min(rec(x + a[i], y, i + 1), rec(x, y + a[i], i + 1));
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	while(cin >> n, n){
		a.RS(n);
		REP(i, n){
			cin >> a[i];
		}
		
		cout << rec(0, 0, 0) << endl;
	}
	
	return 0;
}

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

int n,ans,a[21];
const int INF = 1e+9;

int sum(vector<int> A){
	int s=0;
	for(int i=0 ; i < A.size() ; i++ ){
		s += A[i];
	}
	return s;
}

int solve(int i, vector<int> A, vector<int> B){
	if( i == n ){
		ans = min( ans , abs( sum(A) - sum(B) ) );
	}else{
		A.push_back( a[i] );
		solve( i+1 , A , B );
		A.pop_back();
		B.push_back( a[i] );
		solve( i+1 , A , B );
		B.pop_back();
	}
}

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

C++: #include<iostream>
#include<algorithm>
#include<math>
using namespace std;
int main(){
	double sum,mostnear;
	bool a[20];
	int n,kyas[20],shiftmax,sssum,ansint,i,j;
	while(1){
		scanf("%d",&n);
		if(n==0)
			break;
		for(i=0;i<n;i++){
			scanf("%d",&kyas[i]);
		}
		sort(kyas,kyas+n);
		sum=0;
		for(i=0;i<n;i++){
			sum+=kyas[i];
		}
		sum/=2;
		mostnear = 2100000000;
		shiftmax = 1<<n;
		for(i=0;i<shiftmax;i++){
			for(j=0;j<n;j++){
				a[j] = i&1<<j;
			}
			sssum=0;
			for(j=0;j<n;j++){
				sssum += a[j]*kyas[j];
			}
			mostnear = mostnear>fabs(sssum-sum) ? fabs(sssum-sum) : mostnear;
		}
		printf("%d\n",(int)(mostnear*2));
	}
}

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

using namespace std;

int main(){
  int in;
  while(cin >> in && in){
    long long  data[in];
    for(int i = 0; i < in; i++) cin >> data[i];
    sort(data,data+in);
    reverse(data,data+in);
    long long sum[2] = {0,0};
    for(int i = 0; i < in; i++){
      if(sum[0] > sum[1]) sum[1] += data[i];
      else sum[0] += data[i];
    }
    if(sum[0] > sum[1]) cout << sum[0]-sum[1];
    else cout << sum[1] - sum[0];
    cout << endl;
    //cout << abs(sum[1]-sum[0]) << endl;
  }
  return 0;
}

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

using namespace std;

int main(){
  long long in;
  while(cin >> in && in){
    long long data[in];
    for(int i = 0; i < in; i++) cin >> data[i];
    sort(data,data+in);
    reverse(data,data+in);
    long long sum[2] = {0,0};
    for(int i = 0; i < in; i++){
      if(sum[0] > sum[1]) sum[1] += data[i];
      else sum[0] += data[i];
    }
    cout << abs(sum[1]-sum[0]) << endl;
  }
  return 0;
}