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 <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

int data[100]={};
int mini;
int n;
int SUM;


void dfs(int num,int sum){
    if(num>=n){
        mini=min(mini,abs(SUM-sum-sum));
        return ;
    }
    
    dfs(num+1,sum+data[num]);
    dfs(num+1,sum);
}


int main() {
    while(cin>>n){
        if(n==0)break;
        
        SUM=0;
        mini=99999999;
        rep(i,100) data[i]=0;
        
        rep(i,n){
            cin>>data[i];
            SUM+=data[i];
        }
        
        dfs(0,0);
        cout<<mini<<endl;
    }
    
    return 0;
}

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

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

int solve(int i, int A, int B){
	if( i == n ){
		ans = min( ans , abs(A-B) );
	}else{
		solve( i+1 , A+a[i] , B );
		solve( i+1 , A , B+a[i] );
	}
}

int main(){
	while( cin >> n , n ){
		for(int i=0 ; i < n ; i++ ){
			cin >> a[i];
		}
		ans = INF;
		solve( 0 , 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 suma[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];
    }

    reverse(n,n+in);

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

    sum[0] -= sum[1];
    suma[0] -= suma[1];

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

    if(sum[0] > suma[0]) sum[0] = suma[0];

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

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

int diff( int a, int b )
{
    int c = a - b;
    return c < 0 ? -c : c;
}

int main()
{
    int n;
    bitset<20> b;

    while( cin >> n )
    {
        if( !n )
            break;

        int* a = new int[n];

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

        int limit = 1;
        for( int i = 0; i < n; i++ )
            limit *= 2;

        int min = 1000000000;
        for( int i = 1; i < limit; i++ )
        {
            b = i;
            int s = 0;
            for( int i = 0; i < n; i++ )
                if( b[i] )
                    s += a[i];
            int d = diff( s, sum - s );
            if( min > d )
                min = d;
        }
        
        cout << min << endl;
        delete a;
    }
    return 0;
}

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

using namespace std;

int n, a[30], s;
int ret;

void rec(bool data[30], int deep)
{
	// cout << deep << endl;
	if (deep == n){
		int a_sum = 0, b_sum = 0;
		for (int i = 0; i < n; i++){
			if (data[i]) a_sum += a[i];
			else b_sum += a[i];
		}

		// cout << sum << endl;
		ret = min(ret, (int)abs(a_sum - b_sum));
		return;
	}
	rec(data, deep + 1);
	data[deep] = true;
	rec(data, deep + 1);
	data[deep] = false;
	return;
}

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

		bool data[30];
		fill(data, data + 30, false);
		rec(data, 1);
		data[0] = true;
		rec(data, 1);
		

		cout << ret << endl;
	}

	return 0;
}

C++: #include<iostream>
main(){for(int n,a[20],S,s,m,i,j,t;std::cin>>n,n;printf("%d\n",m)){for(i=S=0;i<n;S+=a[i++])std::cin>>a[i];m=1<<30;for(i=(1<<n);i-->0;t=S-s-s,t=t>0?t:-t,m>t&&(m=t))for(j=s=0;j<n;j++)if(i&1<<j)s+=a[j];}}

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

int n , v[20] , ans = 0;

tr1::unordered_set<int> memo[20];

int X[421] , Y[421] , top;
 
int main(){
	while(scanf("%d\n",&n)&& n){
		int dp2[21] = {0};
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear();
		for(int i = n-1 ; i >= 0 ; i--) dp2[i] = dp2[i+1] + v[i];
		ans = 99999999;
		top = 0;
		X[top] = 0;
		Y[top] = 0;
		while(~top){
			int pos = X[top] , sum = Y[top]; top--;
			if(pos==n) ans = min(ans,sum);
			else{
				if( sum >= dp2[pos] && ans <= sum-dp2[pos]  ) continue;
				X[++top] = pos+1;
				Y[top]   = sum+v[pos];
				X[++top] = pos+1;
				Y[top]   = abs(sum-v[pos]);
			}
		}
		printf("%d\n",ans);
	}
}

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);
		}
	}
	sort(c[a].begin(),c[a].end());
	auto t=upper_bound(c[a].begin(),c[a].end(),(double)sum/2);
	auto qq=t;
	qq--;
printf("%d\n",min(abs(*t-(sum-*t)),abs(*qq-(sum-*qq))));
}
}

C++: #include<iostream>
#include<climits>
using namespace std;
#define MAX 20

int T[MAX], n, sum, minv;

void rec( int cur, int v ){
    if ( cur == n ){
	int d = max(v, sum-v) - min(v, sum-v);
	minv = min(minv, d);
	return;
    }
    rec( cur+1, v);
    rec( cur+1, v+T[cur]);
}

void compute(){
    minv = INT_MAX;
    rec(0, 0);
    cout << minv << endl;
}

main(){
    while( cin >>n && n ) {
	sum = 0;
	for ( int i = 0; i < n; i++ ){
	    cin >> T[i]; sum += T[i];
	}
	compute();
    }
}

C++: #include<bits/stdc++.h>

using namespace std;

int calc_diff(vector<int>& strength, int team) {

  int team_a = 0, team_b = 0;

  for (int i = 0; i < (int) strength.size(); i++) {
    if (team & 1) {
      team_a += strength[i];
    } else {
      team_b += strength[i];
    }
    team >>= 1;
  }

  return abs(team_a - team_b);
}

int main() {

  int size;
  while (cin >> size && size != 0) {

    vector<int> strength(size);
    for (int i = 0; i < size; i++) {
      cin >> strength[i];
    }

    int minDiff = INT_MAX;
    for (int i = 0; i < (1<<size); i++) {
      minDiff = min(minDiff, calc_diff(strength, i));
    }

    cout << minDiff << endl;
  }
  
  return 0;
}