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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int a[30];

int main() {
	int n;
	while(n=in()){
		int i,sum=0,A;
		for(i=0;i<n;i++){
			cin>>a[i];
			sum+=a[i];
		}
		int res=100100100;
		for(i=0;i<1<<n;i++){
			A=0;
			int j;
			for(j=0;j<n;j++){
				if(i>>j&1){
					A+=a[j];
				}
			}
//cout<<i<<" "<<A<<endl;
			chmin(res,abs(sum-2*A));
		}
		cout<<res<<endl;
	}
	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, 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>
#define INF (1<<21)
using namespace std;

int n,t[20],mi,a[20];

void rec(int p){
  int a1,a2;
  if(p == n){
    a1 = 0;
    a2 = 0;
    for(int i=0;i<n;i++){
      if(t[i] == 1) a1 += a[i];
      else a2 += a[i];
    }
    if(a1 - a2 > 0 && a1 - a2 < mi) mi = a1 - a2;
    else if(a2 - a1 > 0 && a2 - a1 < mi) mi = a2 - a1;
    return;
  }

    t[p] = 2;
    rec(p+1);
    t[p] = 1;
    rec(p+1);

}

int main(){
  int i,j;
  while(1){
    cin >> n;
    if(n == 0) break;
    for(i=0;i<n;i++){
      cin >> a[i];
      t[i] = 0;
    }

    mi = INF;
    rec(0);
    cout << mi << endl;

  }
  return 0;
}

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

int main(void){
  int n;
  while(cin >> n && n){
    int a[n],sum,ret = 1<<29;
    for(int i=0;i<n;i++) cin >> a[i];
    sum = accumulate(a,a+n,0);
    for(int i=1;i<(1<<n)-1;i++){
      int tmp=0;
      for(int j=0;j<n;j++){
	if(i&(1<<j)) tmp+=a[j];
      }
      ret = min(ret, abs(tmp - (sum-tmp)));
    }
    cout << ret << endl;
  }
}

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

using namespace std;
int abs(int a)
{
	return(a < 0 ? -a : a);
}

void solve()
{
	int n;
	while(cin >> n, n)
	{
		vector<int> Vec(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Vec[i];
		}
		int mi = 999999999;
		int a = 0;
		int b = 0;
		for(int i = 0; i < (1 << n); ++i)
		{
			a = b = 0;
			for(int j = 0; j < n; ++j)
			{
				if((1 << j) & i)
				{
					a += Vec[j];
				}
				else
				{
					b += Vec[j];
				}
			}
			mi = min(mi, abs(a - b));
		}
		cout << mi << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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

int N, n[21];

int dfs(int suma, int sumb, int k){
    int a, b;
    if(k >= N)   return abs(suma - sumb);
    a = dfs(suma+n[k], sumb, k+1);
    b = dfs(suma, sumb+n[k], k+1);
    return min(a, b);
}

int main(void){
    while(cin >> N){
        if(N==0)    break;
        for(int i=0; i<N; i++){
            cin >> n[i];
        }
        cout << dfs(0, 0, 0) << endl;
    }
    return 0;
}

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

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

using namespace std;


int main(void){
	int n;
	while(cin >> n){
		int ans = 100000000;
		int a[32]={0};
		if(!n)break;
		rep(i,n)cin >> a[i];
		
		rep(i,1<<n){
			int ga = 0,gb = 0;
			for(int j = 1,index = 0; j < 1<<n; j = j<<1,index++){
				if(i&j){
					ga+=a[index];
				}else{
					gb+=a[index];
				}
			}
			ans = min(abs(ga-gb), ans);
		}
		
		printf("%d\n", ans);
	}
}

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 <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

#define FOR(i,b,n) for(int i=b;i<n;i++)
#define RFOR(i,b,n) for(int i=n-1;i>=b;i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define LIMIT 10000002
using namespace std;

static const double EPS = 1e-9;
typedef long long ll;
typedef pair<int, int> paii;

int n, allie[1000] = {0};

int solve()
{
	int res = INT_MAX, p = 0;
	bool dp[1000010] = {0};
	
	FOR(j, 0, n)
	{
		p += allie[j];
		
		RFOR(i, 0, 1000010)
		{
			if(( i == 0 || dp[i] == true)&&(i+allie[j] < 1000010))
			{
				dp[i+allie[j]] = true;
			}
		}
	}
	
	FOR(i, 0, p+1)
	{	
		if(dp[i] == true)
			res = min(res, abs((p-i)-i));
	}
	
	return res;
	
}

int main()
{
	while(cin >> n, n)
	{
		FOR(i, 0, n)
		{
			cin >> allie[i];
		}
		
		cout << solve() << endl;
	}
	
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <complex>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		int sum = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
		}
		int res = 1000000000;
		rep(i, (1<<n)) {
			int A = 0;
			rep(j, n) if (i & (1 << j)) A += a[j];
			res = min(res, abs(A - (sum - A)));
		}
		cout << res << endl;
	}
	return 0;
}