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 <algorithm>
#include <cmath>

using namespace std;

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


void dfs(int diff,int k);

int main(){
	
	while(cin>>n){

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

void dfs(int diff,int k){
	if(k==n){
		ans=min(ans,abs(diff));
	}else{
		dfs(diff+a[k],k+1);
		dfs(diff-a[k],k+1);
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n,p[20],s[1<<20],s1[1<<10];

int main(){
	while(cin>>n){
		if(!n)return 0;
		int n2=n/2;
		fill(s1,s1+(1<<n2),0);
		int sum=0;//,dif=1e+9;
		for(int i=0;i<n;i++){cin>>p[i];sum+=p[i];}
		for(int i=0;i<1<<n2;i++){
			for(int j=0;j<n2;j++)if(i>>j&1)s1[i]+=p[j];
		}
		sort(s1,s1+(1<<n2));
		int ans=1e+9;
		for(int i=0;i<1<<(n-n2);i++){
			int ss=0;
			for(int j=0;j<n-n2;j++)if(i>>j&1)ss+=p[j+n2];
			ans=min(ans,min(abs((*lower_bound(s1,s1+(1<<n2),(sum+1)/2-ss)+ss)*2-sum),abs((*(lower_bound(s1,s1+(1<<n2),(sum+1)/2-ss)-1)+ss)*2-sum)));
		}
		/*for(int i=0;i<1<<n;i++){
			int sss=0;
			for(int j=0;j<n;j++)if((i/(1<<j))%2)sss+=p[j];
			dif=min(dif,abs(sss*2-sum));
		}*/
		cout<<ans<<endl;
		//sort(s,s+(1<<n));
		//for(int i=0;i<1<<n;i++)s[i]=s[i]*2-sum;
		/*int lb=0,ub=1<<n-1,prv=0;
		while(ub-lb>1){
			int mid=(lb+ub)/2;
			if(s[mid]==prv)break;
			if(s[mid]>=0)ub=mid;
			else lb=mid;
			prv=s[mid];
		}
		cout<<abs(s[ub])<<endl;*/
		//cout<<min(*lower_bound(s,s+(1<<n),0),abs(*(lower_bound(s,s+(1<<n),0)-1)))<<endl;
	}
}

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

int n,m,suma,sumb;
int data[21];

void dfs(int a, int b,int cnt){
  suma+=a;
  sumb+=b;
  if(cnt==n){ 
    m=min(m,abs(suma-sumb));
  }else{
    dfs(data[cnt],0,cnt+1);
    dfs(0,data[cnt],cnt+1);
  }
  suma-=a;
  sumb-=b;
}
int main(){
  while(1){
    scanf("%d",&n);
    if(!n) break;
    m=10000000,suma=sumb=0;
    for(int i=0;i<n;i++)
      scanf("%d",&data[i]);
    dfs(0,0,0);
    printf("%d\n",m);
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int n, a[23]; vector<int>v;
void f(int k, int sum) {
	if (k == n)v.push_back(sum);
	else {
		f(k + 1, sum);
		f(k + 1, sum + a[k]);
	}
	return;
}
int main() {
	while (cin >> n, n) {
		v.clear();
		int sum = 0;
		for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; }
		f(0, 0);
		int ans = n;
		for (int i = 0; i < v.size(); i++) {
			int v1 = v[i], v2 = sum - v[i];
			ans = min(ans, abs(v1 - v2));
		}
		cout << ans << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

static const int MAX_MEMBERS = 20;

int Members;

vector<int> power;
int seek = 0;

int FightingPower[MAX_MEMBERS], n, res;
const int INF = 1000001;

void dfs(int i, int a_sum, int b_sum) {
	if (i == n) {
		res = min(abs(a_sum - b_sum), res);
	}
	else {
		dfs(i + 1, a_sum + FightingPower[i], b_sum);
		dfs(i + 1, a_sum, b_sum + FightingPower[i]);
	}
}

int main() {
	while (cin >> n && n!=0) {
		for (int i = 0; i<n; i++) {
			cin >> FightingPower[i];
		}
		res = INF;
		dfs(0, 0, 0);
		cout << res << endl;
	}
}

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;
	}
}