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 <cmath>

using namespace std;

int n, mem_index = 0, fight_strength[20] = {0}; //登録者数、何番目の登録か、登録者の戦闘力

int my_min(int now, int a_sum, int b_sum)
{
	if(now == n) return abs(a_sum - b_sum);

	int min_a = my_min(now + 1, a_sum + fight_strength[now], b_sum),
	    min_b = my_min(now + 1, a_sum, b_sum + fight_strength[now]);

	if(min_a > min_b)
		return min_b;
	else if(min_a < min_b)
		return min_a;
	else
		return min_a;
}

int main(void)
{
	while(true)
	{
		cin >> n;
		if(n == 0) break;
		while(mem_index < n)
		{
			cin >> fight_strength[mem_index];
			mem_index++;
		}

		cout << my_min(0, 0, 0) << endl;
		mem_index = 0;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
 
int a[20];
vector<int> b,c;
int m;
int minsa;
 
int co(){
  int sum=0;
  for(int i=0;i<b.size();i++) sum+=b[i];
  for(int i=0;i<c.size();i++) sum-=c[i];
  if(sum<0) sum*=-1;
  return sum;
}
 
void dps(int u){
 
  if(u==m){
    int sa=co();
    if(sa<minsa) minsa=sa;
    else if(minsa==-1) minsa =sa;
  }
  else {
    b.push_back(a[u]);
    dps(u+1);
    b.pop_back();
    c.push_back(a[u]);
    dps(u+1);
    c.pop_back();
  }
}
 
 
int main(){
   
  while(1){
    cin >> m;
    if(m==0) break;
    
    for(int j=0;j<m;j++) cin >>a[j];
      minsa =-1;
      dps(0);
      cout << minsa << endl;
  }
 
  return 0;
}

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

int n;
int A[100];

int saiki(int a, int b, int k){
	if(k==n){
		return abs(a-b);
	}else{
		return min(saiki(a+A[k], b, k+1), saiki(a, b+A[k], k+1));
	}
}

int main(){
	for(;;){
		scanf("%d", &n);
		if(n==0) break;
		for(int i=0; i<n; i++) cin >> A[i];
		cout << saiki(0, 0, 0) << endl;
	}

	return 0;
}

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

int main(){
    int n, a;
    while(cin >> n, n){
        int sum = 0;
        set<int> s;
        s.insert(0);

        for(int i = 0; i < n; i++){
            cin >> a;
            sum += a;
            set<int> next;
            for(int x : s){
                next.insert(x+a);
                next.insert(x);
            }
            s = next;
        }

        int ans = sum;
        for(int x : s)  ans = min(ans, abs(2*x-sum));
        cout << ans << 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=abs(S-s-s),m>t&&(m=t))for(j=s=0;j<n;j++)if(i&1<<j)s+=a[j];}}

C++: #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int n;
    while(cin >> n,n){
        int a = 0,b = 0;
        int t[100] = {0};
        for(int i = 0; i < n; i++){
            cin >> t[i];
        }
        sort(t,t+n);
        for(int i = n-1; i >= 0; i--){
            if(a >= b)b += t[i];
            else a += t[i];
        }
        printf("%d\n",abs(a-b));
    }
    return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr ll inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  while(cin >> n && n){
    vector<ll> v(n);
    ll sum = 0,ans = inf;
    rep(i,n){
      cin >> v[i];
      sum += v[i];
    }
    sort(all(v));
    rep(i,1 << n){
      ll a = 0;
      rep(j,n){
        if(i & (1 << j)){
          a += v[j];
        }
      }
      ll b = sum - a;
      ans = min(ans,abs(a - b));
    }
    cout << ans << endl;
  }
  return 0;
}

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];}
		if(n<4){
			fill(s,s+(1<<n),0);
			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<<dif<<endl;
		}
		else{
		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)/2-ss)+ss)*2-sum),abs((*(lower_bound(s1,s1+(1<<n2),(sum)/2-ss)-1)+ss)*2-sum)));
		}
		cout<<ans<<endl;
		}
	}
}