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 <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int dfs(int n, vector<int> &v, int A, int B){
  if(n >= v.size()) return abs(A - B);
  int ret = dfs(n + 1, v, A + v[n], B);
  ret = min(ret, dfs(n + 1, v, A, B + v[n]));
  return ret;
}

int main() {
  int N;
  while(cin >>N && N){
    vector<int> v(N); REP(i, N) cin >>v[i];
    cout <<dfs(0, v, 0, 0) <<endl;
  }
  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 = sum;
		for (size_t 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 <algorithm>
#include <cmath>

using namespace std;

int N, a[20];

int rec(int n, int b, int c)
{
  if (n < 0) {
    return abs(b - c);
  }

  return min( rec(n - 1, b + a[n], c),
              rec(n - 1, b, c + a[n]));
}

int main()
{
  while (cin >> N && N) {
    
    // read
    for (int i = 0; i < N; i++) {
      cin >> a[i];
    }

    cout << rec(N - 1, 0, 0) << endl;
  }
}

C++: #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){
		int a=0,b=0;
		vector<int> s(n);
	
	for(int i=0;i<n;i++)
		cin>>s[i];
	
	sort(s.begin(),s.end(),greater<int>());
	
	
		for(int i=0;i<n;i++){
			if(s[i]>0){
			if(a>b)
				b+=s[i];
			else
				a+=s[i];
		}
			else{
				if(a>b)
				a+=s[i];
			else
				b+=s[i];
			}
		cout<<abs(a-b)<<endl;
	}
}

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

using namespace std;

int main()
{
	int n;
	
	for(;;)
	{
		vector<int> num;
		cin >> n;
		if(n==0)
			break;
		for(int i=0;i<n;i++)
		{
			int a;
			cin >> a;
			num.push_back(a);
		}
		
		sort(num.begin(),num.end());
		
		int a=0,b=0;
		int a_f=1;
		
		if(num.size()==2)
		{
			cout << sqrt((num[0]-num[1])*(num[0]-num[1])) << endl;
		}
		else
		{
			if(num.size()%2==0)
			{
				for(int i=0,j=num.size()-1;i<j;i++,j--)
				{
					if(a_f==1)
					{
						a+=num[i]+num[j];
						a_f=0;
					}
					else
					{
						b+=num[i]+num[j];
						a_f=1;
					}
				}
			}
			else
			{
				for(int i=1,j=num.size()-1;i<=j || i<num.size();i++,j--)
				{
					if(a_f==1)
					{
						a+=num[i]+num[j];
						a_f=0;
					}
					else
					{
						b+=num[i]+num[j];
						a_f=1;
					}
				}
				if(a<=b)
					a+=num[0];
				else
					b+=num[0];
			}
			cout << "a:" << a << " b:" << b << endl;
			cout << sqrt((a-b)*(a-b)) << endl;
		}
	}
}

C++: #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int main(void){
	double sum,mostnear;
	bool a[20];
	int n,kyas[20],shiftmax,sssum,ansint,i,j;
	while(1){
		scanf("%d",&n);
		if(n==0)
			break;
		for(i=0;i<n;i++){
			scanf("%d",&kyas[i]);
		}
		sort(kyas,kyas+n);
		sum=0;
		for(i=0;i<n;i++){
			sum+=kyas[i];
		}
		sum/=2;
		mostnear = 2100000000;
		shiftmax = 1<<n;
		for(i=0;i<shiftmax;i++){
			for(j=0;j<n;j++){
				a[j] = i&1<<j;
			}
			sssum=0;
			for(j=0;j<n;j++){
				sssum += a[j]*kyas[j];
			}
			mostnear = mostnear>fabs(sssum-sum) ? fabs(sssum-sum) : mostnear;
		}
		printf("%d\n",(int)(mostnear*2));
	}
	return 0;
}

C++: #include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>

using namespace std;

int a[21];
int n;

int solve(int p, int s, int t)
{
  if(p == n) return abs(s-t);
  return min(solve(p+1, s+a[p], t), solve(p+1, s, t+a[p]));
}

int main()
{
  while(scanf("%d", &n)!=EOF && n != 0){
    for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
    printf("%d\n", solve(0, 0, 0));
  }

  return 0;
}

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

int main() {
	int n;
	while (cin >> n, n) {
		vector<int> a(n);
		for (int& i : a) cin >> i;
		int ans = INT_MAX, b = pow(2.0, n);
		for (int i = 0; i < b; i++) {
			bitset<20> c = i;
			int d = 0;
			for (int j = 0; j < n; j++) {
				if (c[j]) d += a[j];
				else d -= a[j];
			}
			ans = min(abs(d), ans);
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <complex>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp make_pair
#define ERASE(v,i) (v).erase(remove(all(v),i),(v).end())
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define debug(x) cerr<<#x<<" = "<<(x)<<endl;
#define LINE cerr<<"LINE: "<<__LINE__<<endl;

inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
template<class T> void preview(T a,T b){for(T it=a;it!=b;++it)cerr<<*it<<" ";cerr<<endl;}

const int INF = 100000000;
const double PI = acos(-1.0), EPS = 1e-10;

int main() {
	for(int N; cin >> N, N;) {
		vector<int> v(N);
		rep(i,N) cin >> v[i];
		int MIN = INF;
		for(int i = 0; i < (1 << N); i++) {
			int a = 0, b = 0;
			for(int j = 0; j < N; j++) {
				if(i & (1 << j)) a += v[j];
				else b += v[j];
			}
			int ab = abs(a - b);
			MIN = min(ab, MIN);
		}

		cout << MIN << endl;
	}
	return 0;
}

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

using namespace std;

int a[100];
int minimum = 1000000000, n, sum;
void dfs(int now,int i){
  if(i == n){
    if(minimum > abs(sum-now*2)){
      minimum = abs(sum-now*2);
      return;
    }
  }
  dfs(now, i+1);
  dfs(now-a[i], i+1);
}
int main(){
  while(1){
    sum = 0;
    cin >> n;
    if(n == 0) break;

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

    sort(a, a+n);

    dfs(sum, 0);

    cout << minimum << endl;
  }

  return 0;
}