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<iostream>
#include<cstdlib>
#include<algorithm>
#include<vector>

using namespace std;

vector<int> ans;
int main(){
	while(1){
		int n;
		cin >> n;
		
		if(n == 0) break;
		
		int a = 0,b = 0;
		int nl[20];
		for(int i = 0;i < n;i++){
			cin >> nl[i];
		}
		
		sort(nl,nl+n);
		reverse(nl,nl+n);
		
		for(int i = 0;i < n;i++){
			if(a > b){
				b += nl[i];
			}else{
				a += nl[i];
			}
		}
		ans.push_back(abs(a-b));
	}
	for(int i = 0;i < ans.size();i++) cout << ans[i] << endl;
	
	return 0;
}

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

int n;
int p[20];
int mind;

void dfs(int a=0, int b=0, int i=0)
{
  if (i == n){
    mind = min(mind, abs(a - b));
    return;
  }
  dfs(a + p[i], b, i + 1);
  dfs(a, b + p[i], i + 1);
}

int main()
{
  while (cin >> n, n){
    for (int i = 0; i < n; i++){
      cin >> p[i];
    }
    mind = INT_MAX;
    dfs();
    cout << mind << endl;
  }
}

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

int main()
{
  int n;
  for(;cin>>n,n;)
    {
      long long int data[n];
      long long int ans=0;
      for(int i=0;i<n;i++)
	{
	  cin>>data[i];
	  ans+=data[i];
	}
      for(long long int i=0;i<(1<<n);i++)
	{
	  //	  cout<<i<<endl;
	  long long int sum[2]={0,0};
	  for(int j=0;j<n;j++)
	    {
	      //	      cout<<(1<<j)<<endl;
	      if((i&(1<<j))==0)
		sum[0]+=data[j];
	      else
		sum[1]+=data[j];
	    }
	  //	  cout<<sum[0]<<" "<<sum[1]<<endl;
	  ans=min(ans,abs(sum[0]-sum[1]));
	  if(ans==0)
	    break;
	}


      cout<<ans<<endl;
    }

}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int a[],int b[],int p,int m,int n,int s){
  int i;
  int c[20];
  int mn=abs(2*s-m);
  if(s<=m){
    for(i=p;i<n;i++){
      if(b[i]==0){
	memcpy(c,b,sizeof(c));
	c[i]=1;
	int t=f(a,c,i+1,m,n,s+a[i]);
	if(mn==-1||mn>t)
	  mn=t;
      }
    }
  }
  return mn;
}
int main(){
  int i;
  int a[20],b[20],n;
  int sm;
  memset(b,0,sizeof(b));
  while(cin>>n&&n){
    sm=0;
    for(i=0;i<n;i++){
      cin>>a[i];
      sm+=a[i];
    }
    cout<<f(a,b,0,sm,n,0)<<endl;
  }
  return 0;
}

C++: //#define LOCAL

#include <fstream>
#include <iostream>
#include <cmath>

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

using namespace std;

int n;
int a[30];
int ans;

void rec(int i, int A, int B)
{
    if (i == n) {
        ans = min(ans, abs(A - B));
        return;
    }

    rec(i + 1, A + a[i], B);
    rec(i + 1, A, B + a[i]);
}

int solve()
{
    ans = 30000000;
    rec(0, 0, 0);
    return ans;
}

int main()
{
#ifdef LOCAL
    ifstream in("input.txt");
    cin.rdbuf(in.rdbuf());
#endif

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

        rep(n) {
            cin >> a[i];
        }

        cout << solve() << endl;
    }
}

C++: #include<iostream>
#include<climits>
#include<cmath>
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 mi=INT_MAX;
        for(int l=0;l<(2<<n);l++){
            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);
        }
        cout<<mi<<endl;
    }
    
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;

int n;
int mn=2000000000;
int a[21];

void dfs(int battle , int depth){
	if(depth==n){
		mn=min( mn , abs(battle));
	} else {
		//cout << "left >battle:" << abs(battle+a[depth]) << " depth:" << depth << endl;
		dfs(battle+a[depth] , depth+1);
		//cout << "right>battle:" << abs(battle-a[depth]) << " depth:" << depth << endl;
		dfs(battle-a[depth] , depth+1);
	}
}


int main(){
	while(cin>>n , n!=0){
		for(int i=0;i<n;++i){
			cin >> a[i];
		}
		dfs(0 , 0);
		cout << mn << endl;
	}
	return 0; 
}

C++: #include <iostream>
#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(cin >> n && n){
		for(int i = 0 ; i < n ; i++) cin >> v[i] , memo[i].clear() ;
		cout << f(0,0) << endl;
	}
}

C++: #include <stdio.h>

int n;
int s;
int a[32];
int mini;

void rec(int teamA, int idx)
{
	if (idx == n){ //終了条件
		int dx = 2 * teamA - s;
		if (dx < 0){
			dx *= -1;
		}
		if (dx < mini){
			mini = dx;
		}
		return;
	}
	//状態の遷移
	rec(teamA + a[idx], idx + 1);
	rec(teamA         , idx + 1);
	return;
}

int main(void)
{
	int i;
	
	while (scanf("%d", &n) && n){
		
		s = 0;
		for (i = 0; i < n; i++){
			scanf("%d", &a[i]);
			s += a[i];
		}
		
		mini = 1000000000;
		rec(0, 0);
		
		printf("%d\n", mini);
	}
	
	return (0);
}

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();
		sort(v,v+n);
		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 <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
#define INF (1<<25)
 
int main(){
	int n , v[20] , X[41] , Y[41] , top;
	while(scanf("%d\n",&n)&& n){
		int dp2[21] = {0};
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]);
		sort(v,v+n,greater<int>());
		for(int i = n-1 ; i >= 0 ; i--) dp2[i] = dp2[i+1] + v[i];
		
		int ans = INF;
		
		top = 0;
		X[top] = 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);
	}
}