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

int main(){
	while(cin>>n){
		if(!n)return 0;
		fill(s,s+(1<<n),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<<n;i++){
			for(int j=0;j<n;j++)if((i/(1<<j))%2)s[i]+=p[j];
			dif=min(dif,abs(s[i]*2-sum));
		}
		cout<<dif<<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 <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int n;
int dfs(int i, vector<int> a, int sum, int max){
  if(i==n) return abs(max-sum*2);
  if(sum*2>max) return abs(max-2*sum);
  return min( dfs(i+1,a,sum+a[i],max), dfs(i+1,a,sum,max));
}
int main(void){
  while(cin>>n){
    if(n==0) break;
    vector<int> a(n);
    REP(i,n) cin>>a[i];
    int s = accumulate(a.begin(),a.end(),0);
    cout<<dfs(0,a,0,s)<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <cmath>
using namespace std;

#define rep(i, n) for(int i = 0; i< n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 29;
const double EPS = 1e-10;


int main(){
  int a[20];
  int n;
  while(cin >> n && n){
    int res = INF;
    rep(i, n) cin >> a[i];
    rep(i, 1 << n){
      int dif = 0;
      rep(j, n){
	if(i & (1 << j)) dif += a[j];
	else dif -= a[j];
      }
      res = min(res, abs(dif));
    }
    cout << res << endl;
  }
  return 0;
}

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

using namespace std;

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

typedef vector<int> vi;

int calc(vi& a,int bit)
{
	int sum[2]={0};
	rep(i,a.size())
		sum[!!((1<<i)&bit)]+=a[i];
	return abs(sum[0]-sum[1]);
}

int main()
{
	for(int n;cin>>n,n;){
		vi a(n);
		rep(i,n)
			cin>>a[i];
		int res=20000000;
		rep(i,1<<n)
			res=min(res,calc(a,i));
		cout<<res<<endl;
    }
	
	return 0;
}

C++: #include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define INF = (1 << 30)
#define rep(i,n) for(int i = 0 ; i < (n) ; ++i)
int n,ANS,vc[20];

void dfs(int rec,int now){
  if(now == n){
    ANS = min(ANS,abs(rec));
    return;
  }
  dfs(rec+vc[now],now+1);
  dfs(rec-vc[now],now+1);
}

int main(){
  while(cin >> n , n){
    ANS = INF;
    rep(i,n) cin >> vc[i];
    dfs(0,0);
    cout << ANS << endl;
  }
}

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

int n, m[20];

int main() {
	while (true) {
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> m[i];
		}
		int a = 1 << n, b, c, d, minx = 1000000000;
		for (int i = 0; i < a; i++) {
			b = 0; c = 0; d = 0;
			for (int j = 1; j < a; j *= 2) {
				if ((i / j) % 2 == 0) {
					b += m[d];
				}
				else {
					c += m[d];
				}
				d++;
			}
			minx = min(minx, abs(b - c));
		}
		cout << minx << endl;
	}
	return 0;
}

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

int main(){
  int n;
  int a[30];

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

    int ans = 30000000;
    for(int i=0;i<(1<<n);i++){
      int A=0,B=0;
      for(int j=0;j<n;j++){
	if((i>>j)&1)A += a[j];
	else B += a[j];
      }
      ans = min(ans,abs(A-B));
    }
    cout << ans << endl;
  }
}

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

int abs(int a){
  return (a>0)?a:-a;
}

int main(){
  int n,total;
  int a[20];
  set<int> prob;

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

    total=0;
    for(int i=0; i<n; i++)
      cin>>a[i],total+=a[i];

    prob.insert(0);

    set<int> c;
    for(int i=0;i<n;i++){
      copy(prob.begin(), prob.end(), insert_iterator<set<int> >(c, c.end()));
      for(set<int>::iterator it=c.begin();it!=c.end();++it){
	if((*it)+a[i] > total/2 + 1) continue;
	prob.insert((*it)+a[i]);
      }
    }

    cout<<abs (total - (*(--prob.end()))*2 )<<endl;

    prob.clear();
    //cout<<"total: "<<total<<endl;
  }
}

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

using namespace std;

int RegNum;//登録者数
int RegATK[20];//登録者戦闘力
int RegATKAll;//登録者戦闘力合計
int PartyATK[2];//パーティー戦闘力

int GetMinATK(int n,int PAATK){
	if (n == 0)return abs(RegATKAll - (2 * PAATK));
	return min(GetMinATK(n - 1, PAATK + RegATK[n]), GetMinATK(n - 1, PAATK));
}

int main(){

	while (1){
		//変数初期化
		PartyATK[0] = 0; PartyATK[1] = 0;
		RegATKAll = 0;

		cin >> RegNum;//登録者数代入
		if (RegNum == 0)break;//0ならbreak
		for (int i = 0; i < RegNum; ++i){//戦闘力代入
			cin >> RegATK[i];
			RegATKAll += RegATK[i];
		}

		cout << GetMinATK(RegNum, 0) << endl;
	}

	return 0;
}

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

int main(){
  int n;
  while(cin>>n,n){
    int a[20],sum=0;
    for(int i=0;i<n;i++)cin>>a[i],sum+=a[i];
    
    S mp[21];
    S::iterator it;
    mp[0].insert(0);
    for(int i=0;i<n;i++)
      for(it=mp[i].begin();it!=mp[i].end();it++)mp[i+1].insert((*it)+a[i]),mp[i+1].insert(*it);

    int ans=1e9;
    for(it=mp[n].begin();it!=mp[n].end();it++)ans=min(ans,sum-(*it));
    cout <<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int abs(int a){return a < 0 ? -a : a;}
int main(){
    int n;
    int a[22];
    while(cin>>n,n){
        for(int i=0;i<n;i++)
            cin>>a[i];
        int mi=1<<21;
        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;
    }
    
}